【发布时间】: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