【发布时间】:2020-09-30 21:12:00
【问题描述】:
我正在尝试使用文本文件中已有的一些链接来提取不同属性的地址。我已经使用 asyncio 库创建了这个脚本。该脚本运行良好,直到遇到该站点抛出的这种类型的page。我还检查了实施代理,但没有运气。虽然它肯定不是验证码页面,但我在使用 asyncio 时经过几次请求后最终得到了该页面。仅供参考,当我去 requests 模块时,我没有遇到那个页面。
我怎样才能摆脱那个错误页面?
这是我在文本文件中使用的几个urls。
我试过了:
import asyncio
import aiohttp
import random
import requests
from bs4 import BeautifulSoup
async def get_text(session,url):
async with session.get(url,ssl=False) as resp:
assert resp.status == 200
print("----------",str(resp.url))
if "Error" in str(resp.url):raise
return await resp.read()
async def get_info(sem,session,link):
async with sem:
r = await get_text(session,link)
soup = BeautifulSoup(r,"html.parser")
try:
address = soup.select_one("h1#mainaddresstitle").get_text(strip=True)
except AttributeError: address = ""
print(address)
async def main():
sem = asyncio.Semaphore(5)
with open("link_list.txt","r") as f:
link_list = [url.strip() for url in f.readlines()]
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) as session:
await asyncio.gather(
*(get_info(sem,session,item) for item in link_list)
)
if __name__ == '__main__':
asyncio.run(main())
PS当脚本超过速率限制时,它应该会遇到像/Property/UsageValidation这样的页面,但不是
/Property/Error/?id=14e53e71-11b1-4f5e-a88c-f8a4721de99e
【问题讨论】:
-
我猜你在某种程度上受到了速率限制,因为你只有在异步发出所有请求时才能获得这些页面。当您的脚本遇到该页面时,您究竟希望您的脚本做什么?重试请求?
-
我不想首先遇到该页面,因为我在使用请求时看不到该页面。我已经提到,即使我实现了代理,我仍然会遇到该页面。所以,我非常怀疑这是因为速率限制。顺便说一句,当网站不喜欢任何机器人时,它就会开始抛出验证码。
-
你被限速了。
标签: python python-3.x web-scraping python-asyncio