美文网首页Crawler
scrapy传参,动态创建items

scrapy传参,动态创建items

作者: 奈斯凸米特 | 来源:发表于2019-08-20 16:15 被阅读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'] = '张三'

相关文章

网友评论

    本文标题:scrapy传参,动态创建items

    本文链接:https://www.haomeiwen.com/subject/bfppsctx.html