萌新又来了 scrapy 启动时能不能传参数

2019-09-16 18:09:37 +08:00
 wersonliu9527
 import scrapy
class ExampleSpider(scrapy.Spider):
    name = 'baidu.com'
    allowed_domains = ['www.baidu.com']

    # start_urls = ['https://www.baidu.com/']

    def __init__(self, key):
        super(ExampleSpider, self).__init__()
        self.key = key

    def start_requests(self):
        url = f'https://www.baidu.com/s?wd={self.key}'

        yield scrapy.Request(url=url, callback=self.mparse)

    def mparse(self, response):
        yield {
            'title': response.xpath('//title/text()').extract_first()
        }

这样传递参数似乎不行

from scrapy.crawler import CrawlerProcess

from test_spider.spiders.example import ExampleSpider

process = CrawlerProcess() 

process.crawl(ExampleSpider(key='ip'))
process.start()

2613 次点击
所在节点    Python
2 条回复
IanPeverell
2019-09-16 18:45:31 +08:00
这种情况可以直接用
process = CrawlerProcess(settings={"key":"ip"})
然后在爬虫里用 self.setting.get("key") 获取
wersonliu9527
2019-09-17 09:30:19 +08:00
@IanPeverell 感谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/601253

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX