本文将介绍如何将抓好的item数据存入到mysql中
声明:本文使用的python交互工具为pymysql并非mysqldb,因为网上使用pymysql存入数据的相关文章较少,所以写了这篇文章供大家参考
环境:ubantu
准备工作:
items.py
将想要抓的数据累出来,下面是我要抓的数据:(我的项目名为wens)
class WensItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
img_url = scrapy.Field()
name = scrapy.Field()
update_time = scrapy.Field()
update_word = scrapy.Field()
author = scrapy.Field()
steam.py(我的爬虫脚本)
当然也别忘了引入
from wens.items import WensItem
至此准备工作全部完成,本文主要阐述如何将抓好的数据存入数据库,对于如何抓取数据不再赘述。
pipelines.py
import pymysql
class writeMysql(object):
def __init__(self):
self.client = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root', #使用自己的用户名
passwd='123', # 使用自己的密码
db='chenguang', # 数据库名
charset='utf8'
)
self.cur = self.client.cursor()
def process_item(self,item,spider):
# sql = 'insert into game(img_url,name,update_time,update_word,author) VALUES (%s,%s,%s,%s,%s)'%(item['img_url'],item['name'],item['update_time'],item['update_word'],item['author'])
#对于上条命令感觉和下面的sql语句相同,但是就是报语法错误,如果有大神知道希望能够指出
sql = 'insert into game(img_url,name,update_time,update_word,author) VALUES (%s,%s,%s,%s,%s)'
lis = (item['img_url'],item['name'],item['update_time'],item['update_word'],item['author'])
self.cur.execute(sql,lis)
self.client.commit()
# self.cur.close()
# self.client.close()
# 对于这里如果关闭的话只能能插入一条数据,也不知道应该在哪里关闭,还没有弄出来,但是不关闭也没影响
return item
settings.py
别忘了在setting中注册
ITEM_PIPELINES = {
'wens.pipelines.writeMysql': 200,
}
最后建立数据库,我是用navicat(懒),创建数据库及表的方法我就不贴出来了,需要注意的是创建的字段名需要和items.py中自己定义的相同。