可以在 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