V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wersonliu9527
V2EX  ›  Python

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

  •  
  •   wersonliu9527 · 2019-09-16 18:09:37 +08:00 · 2579 次点击
    这是一个创建于 1656 天前的主题,其中的信息可能已经有所发展或是发生改变。
     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()
    
    
    2 条回复    2019-09-17 09:30:19 +08:00
    IanPeverell
        1
    IanPeverell  
       2019-09-16 18:45:31 +08:00   ❤️ 1
    这种情况可以直接用
    process = CrawlerProcess(settings={"key":"ip"})
    然后在爬虫里用 self.setting.get("key") 获取
    wersonliu9527
        2
    wersonliu9527  
    OP
       2019-09-17 09:30:19 +08:00
    @IanPeverell 感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2858 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 11:31 · PVG 19:31 · LAX 04:31 · JFK 07:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.