scrapy将爬取到的数据存储到mysql中,使用pymysql

在 pipelines.py中添加如下代码

import pymysql

class JianshuPipeline(object):
    def __init__(self):
        dbparams = {
            'host':'127.0.0.1',
            'port':3306,
            'user':'root',
            'password':'root',
            'database':'jianshu',
            'charset':'utf8'
        }

        self.conn =pymysql.connect(**dbparams)
        self.cursor = self.conn.cursor()
        self._sql = None

    def process_item(self, item, spider):

        self.cursor.execute(self.sql,(item['title'],item['author'],item['pub_time'],item['content']))
        self.conn.commit()
        return item


    @property
    def sql(self):
        if not self._sql:
            self._sql = """
            insert into article (id,title,author,pub_time,content)
            values (null ,%s,%s,%s,%s)
            
            """
            return self._sql
        return self._sql


你可能感兴趣的:(scrapy,python,pymysql)