class MySQLPipeline(object):
def __init__(self):
# 连接数据库
self.connect = pymysql.connect(
host='xxx.xxx.x.xxx', # 数据库地址
port=3306, # 数据库端口
db='mysql', # 数据库名
user='root', # 数据库用户名
passwd='xxxxxxx', # 数据库密码
charset='utf8', # 编码方式
use_unicode=True)
# 通过cursor执行增删查改
self.cursor = self.connect.cursor()
def process_item(self, item, spider):
self.cursor.execute(
"""insert into test(A ,B,C,D,E,F,G,H,I,J,K,L,M) # 使用%s格式化字段值
value (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
(
item['A'],
item['B'],
item['C'],
item['D'],
item['E'],
item['F'],
item['G'],
item['H'],
item['I'],
item['J'],
item['K'],
item['L'],
item['M],))
# 提交sql语句
self.connect.commit()
self.cursor.close()
self.connect.close()
return item # 必须实现返回
作者:WangB

本文详细介绍了如何在Python的Scrapy爬虫框架中集成MySQL数据库,包括安装必要的库,配置数据库连接,以及在爬取过程中存储数据到MySQL的步骤。

678

被折叠的 条评论
为什么被折叠?



