Scrapy 异步问题求助 - V2EX
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
kekeones
V2EX    Python

Scrapy 异步问题求助

  •  
  •   kekeones 2023-12-16 13:16:29 +08:00 1800 次点击
    这是一个创建于 715 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 Scrapy 爬取内容,使用 Pipeline 将处理后内容 POST 到远程。

    使用request是同步的会有阻塞。

    使用scrapy.FormRequest受限于全局DOWNLOAD_DELAY限制,每次 POST 都会有延迟。

    使用 treq ,不确定如何获取内容

    async def process_post(self, url, data): req = treq.post(url, data=data) res = await deferred_to_future(req) return res 

    如何获取 res 的内容,打印是<treq.response._Response 200 'text/html; charset=' unknown size>

    7 条回复    2023-12-28 21:32:04 +08:00
    encro
        1
    encro  
       2023-12-16 13:46:47 +08:00   1
    不看源码?
    看 treq.response._Response 的源码啊!!!
    kekeones
        2
    kekeones  
    OP
       2023-12-16 14:16:03 +08:00
    @encro 谢谢,搞定了,原来我缺少了编码参数
    kekeones
        3
    kekeones  
    OP
       2023-12-18 10:05:32 +08:00
    @encro 刚试了下,不对,res.content(),
    打印出来的是:
    <Deferred at 0x7fb3587834d0 current result: b'{"state":true}'>
    ayugesheng
        4
    ayugesheng  
       2023-12-27 17:56:19 +08:00
    @kekeones 既然都 async 了,推荐直接 aiomysql + aiohttp ,给出一个 aiohttp 的 pipeline 示例:

    import asyncio

    import aiohttp
    from scrapy.utils.defer import deferred_from_coro


    class DemoPipeline:
    def __init__(self) -> None:
    # 一些参数初始化
    pass

    def open_spider(self, spider):
    # 这里可以写入一些非 async 的预备操作,把比如默认参数设置和日志配置等
    return deferred_from_coro(self._open_spider(spider))

    async def _open_spider(self, spider):
    # 这里一般是连接池,async 连接等预备操作
    await asyncio.sleep(0.1)

    async def process_item(self, item, spider):
    # 这里可以使用一些 async 存储库来实现存储逻辑
    ...
    # 看你想 post 到 data 还是 form
    # post_data = json.dumps('{"content": "test"}')
    post_data = {"content": "test"}
    async with aiohttp.ClientSession() as session:
    async with session.post(
    "http://httpbin.org/post", data=post_data
    ) as additional_response:
    # 获取响应内容
    additional_data = await additional_response.text()
    print("additional_data:", additional_data)
    return item

    async def _close_spider(self):
    # 这里一般是 async 连接或连接池关闭逻辑
    await asyncio.sleep(0.1)

    def close_spider(self, spider):
    return deferred_from_coro(self._close_spider())

    注意:
    使用以上代码时,需要在 settings.py 中或者 custom_settings 中配置 "TWISTED_REACTOR": "twisted.internet.asyncioreactor.AsyncioSelectorReactor"
    ayugesheng
        5
    ayugesheng  
       2023-12-27 17:58:27 +08:00
    @kekeones 推荐直接 aiomysql + aiohttp ,给出一个 aiohttp 的 pipeline 示例:

    ```
    import asyncio

    import aiohttp
    from scrapy.utils.defer import deferred_from_coro


    class DemoPipeline:
    def __init__(self) -> None:
    # 一些参数初始化
    pass

    def open_spider(self, spider):
    # 这里可以写入一些非 async 的预备操作,把比如默认参数设置和日志配置等
    return deferred_from_coro(self._open_spider(spider))

    async def _open_spider(self, spider):
    # 这里一般是连接池,async 连接等预备操作
    await asyncio.sleep(0.1)

    async def process_item(self, item, spider):
    # 这里可以使用一些 async 存储库来实现存储逻辑
    ...
    # 看你想 post 到 data 还是 form
    # post_data = json.dumps('{"content": "test"}')
    post_data = {"content": "test"}
    async with aiohttp.ClientSession() as session:
    async with session.post(
    "http://httpbin.org/post", data=post_data
    ) as additional_response:
    # 获取响应内容
    additional_data = await additional_response.text()
    print("additional_data:", additional_data)
    return item

    async def _close_spider(self):
    # 这里一般是 async 连接或连接池关闭逻辑
    await asyncio.sleep(0.1)

    def close_spider(self, spider):
    return deferred_from_coro(self._close_spider())
    ```

    注意:
    使用以上代码时,需要在 settings.py 中或者 custom_settings 中配置 "TWISTED_REACTOR": "twisted.internet.asyncioreactor.AsyncioSelectorReactor"

    以上代码乱了,无语,重发一次。
    ayugesheng
        6
    ayugesheng  
       2023-12-27 18:00:06 +08:00
    不好意思,v2ex 是不支持 markdown 吗,不怎么在论坛发东西。
    kekeones
        7
    kekeones  
    OP
       2023-12-28 21:32:04 +08:00
    好的,谢谢了哈,后面用了 treq 来处理了。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5753 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 02:02 PVG 10:02 LAX 18:02 JFK 21:02
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86