我已经在 python 中编写了一个脚本,使用asyncio与aiohttp库的关联来解析弹出框中的名称,该弹出框是在点击本网站表格内不同机构信息的联系信息按钮时启动的。该网页显示 513 页的表格内容。我too many file descriptors in select()在尝试时遇到了这个错误,asyncio.get_event_loop()但是当我遇到这个线程时,我可以看到有一个建议可以asyncio.ProactorEventLoop()用来避免这种错误,所以我使用了后者,但注意到,即使我遵守了建议,脚本也会收集仅从几页命名,直到引发以下错误。我怎样才能解决这个问题?raise client_error(req.connection_key, exc) from excaiohttp.client_exceptions.ClientConnectorError: Cannot connect to host www.tursab.org.tr:443 ssl:None [The semaphore timeout period has expired]这是我到目前为止的尝试:import asyncioimport aiohttpfrom bs4 import BeautifulSouplinks = ["https://www.tursab.org.tr/en/travel-agencies/search-travel-agency?sayfa={}".format(page) for page in range(1,514)]lead_link = "https://www.tursab.org.tr/en/displayAcenta?AID={}"async def get_links(url): async with asyncio.Semaphore(10): async with aiohttp.ClientSession() as session: async with session.get(url) as response: text = await response.text() result = await process_docs(text) return resultasync def process_docs(html): coros = [] soup = BeautifulSoup(html,"lxml") items = [itemnum.get("data-id") for itemnum in soup.select("#acentaTbl tr[data-id]")] for item in items: coros.append(fetch_again(lead_link.format(item))) await asyncio.gather(*coros)async def fetch_again(link): async with asyncio.Semaphore(10): async with aiohttp.ClientSession() as session: async with session.get(link) as response: text = await response.text() sauce = BeautifulSoup(text,"lxml") try: name = sauce.select_one("p > b").text except Exception: name = "" print(name)简而言之,该process_docs()函数所做的就是data-id从每个页面中收集数字,并将其作为此https://www.tursab.org.tr/en/displayAcenta?AID={}链接的前缀来重复使用,从而从弹出框中收集名称。一个这样的 id is8757和一个这样的合格链接https://www.tursab.org.tr/en/displayAcenta?AID=8757。顺便说一句,如果我将links变量中使用的最高数字更改为 20 或 30 左右,它会顺利进行。
添加回答
举报
0/150
提交
取消