如何在Aiohttp中使用Proxy

可以在 session.get() 中设置 proxy


async with session.get(url, proxy=your_proxy_url) as response:
    return BeautifulSoup(await response.content, 'html.parser')

如果IP需要认证,可以这样设置:

proxy = 'http://your_user:your_password@your_proxy_url:your_proxy_port'
async with session.get(url, proxy=proxy) as response:
    return BeautifulSoup(await response.content, 'html.parser')

或者是这样设置:

proxy = 'http://your_proxy_url:your_proxy_port'
proxy_auth = aiohttp.BasicAuth('your_user', 'your_password')
async with session.get(url, proxy=proxy, proxy_auth=proxy_auth) as response:
    return BeautifulSoup(await response.content, 'html.parser')

参考https://docs.aiohttp.org/en/stable/client_advanced.html#proxy-support

posted @ 2022-01-25 02:16  neightChen  阅读(1615)  评论(0)    收藏  举报