使用同一个ClientSession获取多个不同的url
Using the same ClientSession to get multiple different urls
通常我在请求中编写代码,因此我对 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 抓取该列表源代码中的手表 link。然后它使用 GET 请求将列表添加到观察列表。 add to watch list 必须有2个GET请求link,否则不会真正添加。
那是在请求中,但现在我需要在 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)运行 完美地创建了帐户。但是当涉及到监视列表位时,它就不起作用了。这可能是什么原因?
我试了很多次,终于发现cookie有问题。并且您需要将代码更改为 aiohttp.ClientSession(headers=headers)
。顺便说一句,真相可能在 cookies 中,其中将 ;
转换为 3
Not 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())
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 或任何类似的东西:)
通常我在请求中编写代码,因此我对 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 抓取该列表源代码中的手表 link。然后它使用 GET 请求将列表添加到观察列表。 add to watch list 必须有2个GET请求link,否则不会真正添加。
那是在请求中,但现在我需要在 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)运行 完美地创建了帐户。但是当涉及到监视列表位时,它就不起作用了。这可能是什么原因?
我试了很多次,终于发现cookie有问题。并且您需要将代码更改为 aiohttp.ClientSession(headers=headers)
。顺便说一句,真相可能在 cookies 中,其中将 ;
转换为 3
Not
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())
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 或任何类似的东西:)