我想用递归的方式查询一个网页下面的所有后续页面 /index.php /index_2.php 这样。
pages = set()
def searchAllPages(url, name):
'''获得所有页面链接'''
global pages
ObjUrl = BaseUrl + url
regular_str = r"\/%s\/index_*[0-9]*\.php" % name
time.sleep(1)
try:
r = requests.get(ObjUrl)
except (requests.ConnectionError, requests.HTTPError) as e:
return
else:
bsObj = BeautifulSoup(r.text,'lxml')
links = bsObj.find_all('a', href=re.compile(regular_str))
links = [i.attrs['href'] for i in links]
for link in links:
if link not in pages:
# 新页面
pages.add(link)
searchAllPages(link, name)
运行后报错 提示
equests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None)", ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))
请问这个问题是如何引起的?
我该如何解决?
已经在多处搜索这个问题的原因。始终没找到符合我的答案。
但不是每次都失败的样子。。
找到一个比较符合我想法的答案,就是可能我的访问量和速度太频繁,被对面认为是攻击而关闭。
请问还有没 其他更合理的解释?
Maybe the other party’s server has done anti-crawling. Try adding the header manually to requests
You didn’t give a specific Url address, so it’s hard to test~
You can try to rewrite it into multi-threading and use a queue to manage the URLs that need to be crawled.
You can add header and try to access again.
Thanks for the answer. After adding the header, I ran it 3 or 4 times and no errors were reported.
Like it!