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'] = '张三'
网友评论