0
我有一个抓取程序,它从多个网站获取数据并将信息更新到一个mysql表中。我正在使用scrapy编写爬虫程序。爬虫程序会插入/更新大量列。是否可以批量插入/更新scrapy中的项目?Scrapy批量插入
我有一个抓取程序,它从多个网站获取数据并将信息更新到一个mysql表中。我正在使用scrapy编写爬虫程序。爬虫程序会插入/更新大量列。是否可以批量插入/更新scrapy中的项目?Scrapy批量插入
不知道你问,但你可以设置MySQL管道在scrapy项目pipelines.py,像这样:
class SQL(object):
def __init__(self):
self.conn = MySQLdb.connect(user='user', passwd='pass', db='DB', host='servername', charset='utf8')
self.cursor = self.conn.cursor()
def process_item(self, item, spider):
try:
self.cursor.execute('INSERT INTO table (month, year, date) VALUES (%s,%s,%s)', (item['month'], item['year'], item['date']))
self.conn.commit()
except MySQLdb.Error, e:
print item
print "Error %d: %s" % (e.args[0], e.args[1])
return item
然后settings.py中启用它
ITEM_PIPELINES = {'PROJECTNAME.pipelines.SQL': 300}
如果scrapy一次写入sql一条记录,与在一个批量查询中写入10,000条记录相比,这将花费大量时间。我想知道当scrapy将记录刷新到MySQL时,我是否可以指定这样的数字(如10,000) – katrix 2015-02-08 04:35:34