推荐学习书目
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
nyxsonsleep
V2EX  ›  Python

scrapy 爬取数据的问题

  •  
  •   nyxsonsleep · Jan 12, 2022 · 2351 views
    This topic created in 1600 days ago, the information mentioned may be changed or developed.

    假设要爬取 a 、b 、c url 的页面。但是 b 页面的 url 是从 a url 中获取的,类似的 c 中 b 来。 那么是不是只能通过 yield Requests 嵌套回调函数的形式来解决? 还是有更好的方法呢?

    类似如下:

    class spider:
    	def start_requests:
        	yield Requests(url=a)
            
        def parse:
        	......
            yield Requests(url=b,...,callback=func_b )
            
        def func_b:
        	...
            yield Requests(url=c,...,callback=func_c )
            
        def func_c:
        	...
            return item   
    

    另外问下,以上这种情形应该用什么名词。。。

    3 replies    2022-01-15 14:50:13 +08:00
    qsnow6
        1
    qsnow6  
       Jan 12, 2022   ❤️ 1
    这就是标准的抓取流程,必须通过 yield 抛出 Request 交给 scrapy 异步来调度,抓取好了会调用 callback 。
    如果不走 yield 、callback 这个流程那不就是同步了吗?这样的话会失去这个框架很多的特色。
    qsnow6
        2
    qsnow6  
       Jan 12, 2022
    scrapy 部分功能支持协程的写法: https://docs.scrapy.org/en/latest/topics/coroutines.html#
    nyxsonsleep
        3
    nyxsonsleep  
    OP
       Jan 15, 2022
    @qsnow6 非常感谢,其实我是以为会有什么简单的链式的获取的函数。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2750 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 06:26 · PVG 14:26 · LAX 23:26 · JFK 02:26
    ♥ Do have faith in what you're doing.