scrapy 数据存储mysql
2017-05-10 17:29
387 查看
#spider.py from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Rule from Cwpjt.items import CwpjtItem class FulongSpider(CrawlSpider): name = 'fulong' allowed_domains = ['sina.com.cn'] start_urls = ['http://sina.com.cn/'] 'http://news.sina.com.cn/c/2017-05-09/doc-ifyeycte9324112.shtml' rules = ( Rule(LinkExtractor(allow=('.*?/[0-9]{4}.[0-9]{2}.[0-9]{2}.doc-.*?shtml'),allow_domains=('sina.com.cn')), callback='parse_item', follow=True), ) def parse_item(self, response): i = CwpjtItem() i['name']=response.xpath('/html/head/title/text()').extract() i['kws'] = response.xpath('/html/head/meta[@name="keywords"]/@content').extract() #i['domain_id'] = response.xpath('//input[@id="sid"]/@value').extract() #i['name'] = response.xpath('//div[@id="name"]').extract() #i['description'] = response.xpath('//div[@id="description"]').extract() return i
pipeline
import pymysql from pymysql import connections class CwpjtPipeline(object): def __init__(self): self.conn = pymysql.connect(host='127.0.0.1',user='root',passwd='123456',db ='mydb') self.cursor = self.conn.cursor() def process_item(self, item, spider): name = item['name'][0] kws = item['kws'][0] sql ="insert into hehe(title,kws) VALUES(%s,%s)" self.cursor.execute(sql,(name,kws,)) self.conn.commit() return item def close_spider(self,spider): self.conn.close()
item
import scrapy class CwpjtItem(scrapy.Item): # define the fields for your item here like: name = scrapy.Field() kws = scrapy.Field()
相关文章推荐
- python,scrapy爬虫sql之爬取数据存储到mysql的piplelines.py配置
- scrapy将数据存储到mysql中
- Scrapy爬数据并存储到mysql中
- Scrapy爬数据并存储到mysql中
- 用scrapy-redis爬去新浪-以及把数据存储到mysql\mongo
- 修改MySQL默认的数据文件存储目录
- mysql数据据存储引擎InnoDB和MyISAM的优势及区别
- Mysql数据备份以及异地存储
- MySql 存储过程使用游标循环插入数据示例
- 用hibernate进行MYSQL数据存储时遇到的中文编码问题及其解决方
- 在Mysql中用 LONGBLOB 类型存储二进制数据
- mysql中要存储汉字要将数据类型定义为什么?
- 可插式数据存储引擎 MySQL走向企业级
- 在Mysql中用 LONGBLOB 类型存储二进制数据
- MySQL存储过程动态创建表,数据分表
- MySQL动态创建表,数据分表的存储过程
- mysql数据据存储引擎InnoDB和MyISAM的优势及区别
- MySQL 存储 schema-less的数据
- MySQL的数据存储格式:按照读写比的选择
- 如何修改mysql数据文件存储位置