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
nyxsonsleep
V2EX  ›  Python

scrapy 爬取数据的问题

  •  
  •   nyxsonsleep · 2022-01-12 09:58:53 +08:00 · 1789 次点击
    这是一个创建于 829 天前的主题,其中的信息可能已经有所发展或是发生改变。

    假设要爬取 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 条回复    2022-01-15 14:50:13 +08:00
    qsnow6
        1
    qsnow6  
       2022-01-12 10:26:33 +08:00   ❤️ 1
    这就是标准的抓取流程,必须通过 yield 抛出 Request 交给 scrapy 异步来调度,抓取好了会调用 callback 。
    如果不走 yield 、callback 这个流程那不就是同步了吗?这样的话会失去这个框架很多的特色。
    qsnow6
        2
    qsnow6  
       2022-01-12 10:29:25 +08:00
    scrapy 部分功能支持协程的写法: https://docs.scrapy.org/en/latest/topics/coroutines.html#
    nyxsonsleep
        3
    nyxsonsleep  
    OP
       2022-01-15 14:50:13 +08:00
    @qsnow6 非常感谢,其实我是以为会有什么简单的链式的获取的函数。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1963 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:17 · PVG 00:17 · LAX 09:17 · JFK 12:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.