【问题标题】:Using the same ClientSession to get multiple different urls使用同一个ClientSession获取多个不同的url
【发布时间】:2018-10-29 15:20:34
【问题描述】:

通常我在请求中编码,因此我对 aiohttp 没有太多经验。但由于请求被阻塞,我必须使用 aiohttp。

那么我的代码在请求中的样子:

#Account gen code is here using requests 

r = requests.get(product_link)

watch_link = soup(r.text, "html.parser").find("div", {"id": "vi-atl-lnk"}).a["href"]

r = requests.get(watch_link)
r = requests.get(watch_link)   

所以它的作用是进入 Ebay 列表,然后使用 BS4 抓取该列表源代码中的观看链接。然后它使用 GET 请求将列表添加到监视列表。添加到监视列表链接上必须有 2 个 GET 请求,否则它实际上不会添加它。

那是在请求中,但现在我需要在 aiohttp 中编写它。我得到的最接近的是:

session = aiohttp.ClientSession()

async def main():
    #Account gen code is here using aiohttp and session 
    async with session.get(product_link) as resp:
         r = await resp.text()
         watch_link = soup(r, "html.parser").find("div", {"id": "vi-atl-lnk"}).a["href"]
    async with session.get(watch_link) as respp:   
         time.sleep(.1)
    async with session.get(watch_link) as resp:   
         time.sleep(.1)

 loop = asyncio.get_event_loop()
 loop.run_until_complete(main())

我试过这个,它为我运行,但它没有将该项目添加到监视列表中。上面的代码(未显示,因为它与此问题 AFAIK 无关)运行完美并创建了帐户。但是当涉及到监视列表位时,它不起作用。这可能是什么原因?

【问题讨论】:

  • 我发现问题出现在watch_link,无效。

标签: python beautifulsoup python-requests python-asyncio aiohttp


【解决方案1】:

我尝试了很多次,最后发现它在 cookie 上有问题。您需要将代码更改为aiohttp.ClientSession(headers=headers)。顺便说一句,真相可能在 cookie 中,将 ; 转换为 \073

不是aiohttp.ClientSession(headers=headers,cookies=cookies)

那里是我整理出来的代码。

import aiohttp
import asyncio
from bs4 import BeautifulSoup as soup

product_link = ""

cookies = {"Cookie":"_ga=GA1.2.808...."}
headers = {"Connection": "keep-alive"}
headers.update(cookies)

async def main():
    #Account gen code is here using aiohttp and session 
    async with aiohttp.ClientSession(headers=headers) as sessions:

        async with sessions.get(product_link) as resp:
            r = await resp.text()
            watch_link = soup(r, "lxml").find("div", {"id": "vi-atl-lnk"}).a.get("href")
            print(watch_link)

        async with sessions.get(watch_link) as resp:
            pass

        async with sessions.get(watch_link) as resp:
            pass


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

【讨论】:

  • 感谢您的帮助,但我很困惑。为什么 aiohttp 需要 headers/cookies 而 requests 不需要?
  • 不,你需要cookies来让网站确认你是谁,否则它几乎不知道他应该添加哪个监视列表。(我再次尝试了请求和非cookies,它失败了)跨度>
  • @warkun5400 顺便说一句,您是如何得到 requests doesn't 的结果的?
  • 我认为您失败的原因是因为您在尝试添加到监视列表之前没有创建帐户并且没有登录它
  • @warkun5400 这意味着你仍然需要一些东西来显示你是谁,那就是 cookie。我没有登录功能,所以我必须自己添加cookie。所以我认为我们做了同样的事情,只是流程不同
【解决方案2】:
        session = aiohttp.ClientSession()
        async with session.post(link,data=payload,headers=headers) as resp:
            print("Created account with email " + random_catchall)
        async with session.get(product_link,headers=headers) as response:
            r = await response.text()
            watch_link = soup(r, "html.parser").find("div", {"id": "vi-atl-lnk"}).a["href"]

            print("Connected to product")

        async with session.get(watch_link,headers=headers) as respp:
            print("Sucessfully added to watchlist")
        async with session.get(watch_link,headers=headers) as respp:
            print("Added to watch list")


        await session.close()

这最终对我有用。不需要 cookie 或类似的东西 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2016-11-19
    • 2023-03-16
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    相关资源
    最近更新 更多