scrapy---- MySQLデータベースにデータを保存する
1095 ワード
1.pipelines.pyで自分のpipelineをカスタマイズ
2.settingsで自分のpipelineを開く
import pymysql
class PymysqlPipeline(object):
def __init__(self):
#
self.connect = pymysql.connect(
host='localhost',
db='bole',
user='root',
passwd='123456',
charset='utf8',
port=330,
use_unicode=True)
self.cursor = self.connect.cursor()
def process_item(self, item, spider):
cursor = self.cursor
sql = 'insert into bole(title, datetime, category, content, dianzanshu, shoucanshu, pinglunshu) values (%s,%s,%s,%s,%s,%s,%s)'
cursor.execute(sql, (
item['title'], item['datetime'], item['category'], item['content'], item['dianzanshu'],
item['shoucanshu'],
item['pinglunshu']))
self.connect.commit()
return item
2.settingsで自分のpipelineを開く
ITEM_PIPELINES = {
'Bole.pipelines.PymysqlPipeline': 1,
}