我正在尝试使用aynscio
和aiohttp
软件包来请求网页。但是,网页响应为:
<p class="warning-title"> Please upgrade your web browser. </p> <br/>
<p class="p-top-30">This browser version is outdated, and may not be fully compatible with our website. Please upgrade to a newer version or use another browser. </p>
它实际上不会加载我尝试访问的页面,而是加载主页。
代码
from fake_useragent import UserAgent
import ssl
from bs4 import BeautifulSoup
import asyncio
import aiohttp
ua = UserAgent()
hdr = {'User-Agent': str(ua.chrome),
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.5',
'Connection': 'keep-alive'}
ssl_ctx = ssl.create_default_context()
ssl_ctx.check_hostname = False
ssl_ctx.verify_mode = ssl.CERT_NONE
url = '...'
async def parse_website(session):
async with session.get(url) as response:
html = await response.text()
soup = BeautifulSoup(html, 'html.parser')
print(soup)
async with asyncio.Semaphore(3):
async with aiohttp.TCPConnector(ssl=ssl_ctx, limit=None) as connector:
async with aiohttp.ClientSession(connector=connector, headers=hdr) as session:
for i in range(1):
await parse_website(session)
我尝试在倒数headers
的第三行中不包含async with aiohttp.ClientSession(connector=connector) as session:
参数,但是随后的响应是我没有足够长时间等待验证码。因此,我必须使用headers
参数来绕过验证码,但是我始终得到一个Please upgrade your browser
响应。我还尝试将cookies={}
添加到同一行async with aiohttp.ClientSession(connector=connector, headers=hdr, cookies={}) as session:
,但得到相同的原始响应,表示浏览器已过时。
我在这里也只显示一个网址请求。完成这项工作后,我将可以扩展到数千个,这就是为什么我试图使用asyncio
和aiohttp
软件包来完成这项工作的原因。
有人可以告诉我我在哪里错吗?