python用requests递归查询页面 报错 ChunkedEncodingError
大家讲道理
大家讲道理 2017-04-18 09:43:00
0
4
901

我想用递归的方式查询一个网页下面的所有后续页面 /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))

请问这个问题是如何引起的?
我该如何解决?

已经在多处搜索这个问题的原因。始终没找到符合我的答案。

但不是每次都失败的样子。。
找到一个比较符合我想法的答案,就是可能我的访问量和速度太频繁,被对面认为是攻击而关闭。
请问还有没 其他更合理的解释?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
PHPzhong

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!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!