解决Scrapy使用pipline保存到数据库后返回None
  TEZNKK3IfmPf 2023年11月14日 34 0

这也不算报错哈

解决方法

process_item处理完成后返回item即可:

return item

案例分析

比如下面的pipeline

class MyPipeline(object):
    def __init__(self):
        host = "127.0.0.1"
        user = "xxx"
        password = "xxx"
        db = "xxx"

        self.conn = pymysql.connect(host=host, user=user, password=password, database=db)
        self.cursor = self.conn.cursor()

    def process_item(self, item, spider):
        if isinstance(item, StockItem):
            sql = "INSERT INTO stock_code(code,company,stock_type) VALUES ('%s','%s','%s');" % \
                  (item["stock_code"], item["company_name"], item["stock_type"])
            self.cursor.execute(sql)
            self.conn.commit()
            return item # 如果去掉这一行,会打印None,返回item即可
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

TEZNKK3IfmPf