在Python3.8.x上的Scrapy2.4.x中,我生成了一个条目,目的是将一些统计数据保存到数据库中。刮刀还有另一个物品也会被放弃
虽然项目名称出现在主脚本“StatsItem”中,但它在另一个类中丢失。我正在使用项的名称来决定调用哪个方法:
在scraper.py中:
import scrapy
from crawler.items import StatsItem, OtherItem
class demo(scrapy.Spider):
def parse_item(self, response):
stats = StatsItem()
stats['results'] = 10
yield stats
print(type(stats).__name__)
# Output: StatsItem
print(stats)
# Output: {'results': 10}
在管道中.py
import scrapy
from crawler.items import StatsItem, OtherItem
class mysql_pipeline(object):
def process_item(self, item, spider):
print(type(item).__name__)
# Output: NoneType
if isinstance(item, StatsItem):
self.save_stats(item, spider)
elif isinstance(item, OtherItem):
# call other method
return item
第一个类中的print输出为“StatsItem”,而管道中的print输出为“NoneType”,因此永远不会调用save_stats()方法
我对Python非常陌生,因此可能有更好的方法来实现这一点。我知道没有错误消息或异常。非常感谢您的帮助
不能在函数imo之外使用
yield
我终于找到了问题所在。特定的爬虫程序几乎与所有其他没有此问题的爬虫程序相同,但有一个例外,我正在自定义设置项目管道:
删除此项后,修复了该问题
相关问题 更多 >
编程相关推荐