简体   繁体   English

使用相同的 ClientSession 获取多个不同的 url

[英]Using the same ClientSession to get multiple different urls

Normally I code in requests so as a consequence I don't have much experience with aiohttp.通常我在请求中编码,因此我对 aiohttp 没有太多经验。 But since requests is blocking I have to use aiohttp.但是由于请求被阻塞,我必须使用 aiohttp。

So what my code looks like in requests:那么我的代码在请求中的样子:

#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)   

So what this does is that it goes to an Ebay listing and then uses BS4 to scrape the watch link that is in that listing's source code.因此,它的作用是转到 Ebay 列表,然后使用 BS4 抓取该列表源代码中的观看链接。 It then uses GET request to add the listing to the watch list.然后它使用 GET 请求将列表添加到监视列表中。 There have to be 2 GET requests on the add to watch list link, otherwise it won't actually add it.添加到监视列表链接上必须有 2 个 GET 请求,否则它实际上不会添加它。

Well that was in requests but now I need to write this in aiohttp.那是在请求中,但现在我需要在 aiohttp 中编写它。 The closest I got is this:我得到的最接近的是这个:

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())

I tried this and it ran for me, however it didn't add the item to the watch list.我试过了,它为我运行,但是它没有将该项目添加到监视列表中。 The code above this (not shown as it is not relevant to this problem AFAIK) ran perfectly and made the account.上面的代码(未显示,因为它与此问题 AFAIK 无关)运行完美并创建了帐户。 But it is not working when it comes to the watch list bit.但是当涉及到监视列表位时它不起作用。 What could be the reason for this?这可能是什么原因?

I try so many times and finally found it got an issue on cookies.我尝试了很多次,最后发现它在 cookie 上有问题。 And you need to change your code to aiohttp.ClientSession(headers=headers) .您需要将代码更改为aiohttp.ClientSession(headers=headers) btw The truth may be in the cookies , where convert ;顺便说一句,真相可能在 cookie 中,在那里 convert ; to \\073\\073

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

There the code i sorted out.那里是我整理出来的代码。

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()

This ended up working for me.这最终对我有用。 No need for cookies or anything of that sort :)不需要饼干或任何类似的东西:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM