Crawler

scrapy传参,动态创建items

2019-08-20  本文已影响0人  奈斯凸米特

scrapy从启动文件传递参数到spiders

start.py传参

# 这里的c为参数
c = 'xxx'
os.system('scrapy crawl XXX -a country="%s"' % c)

spiders.py 接收参数

    def __init__(self, cou='', **kwargs):
        super().__init__()
        self.cou = cou

创建动态items:

from scrapy import Field

items = MyItems()
# 创建名为name的字段
items.fields['name'] = Field()
# 或者用下边这种写法
items.fields['name'] = scrapy.Field()
# 赋值
items['name'] = '张三'
上一篇 下一篇

猜你喜欢

热点阅读