请求中的URL超出了最大重试次数

我正在尝试获取App Store> Business的内容:


import requests

from lxml import html


page = requests.get("https://itunes.apple.com/in/genre/ios-business/id6000?mt=8")

tree = html.fromstring(page.text)


flist = []

plist = []

for i in range(0, 100):

    app = tree.xpath("//div[@class='column first']/ul/li/a/@href")

    ap = app[0]

    page1 = requests.get(ap)

当我尝试range用(0,2)它工作,但是当我把range在100的IT显示了这个错误:


Traceback (most recent call last):

  File "/home/preetham/Desktop/eg.py", line 17, in <module>

    page1 = requests.get(ap)

  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 55, in get

    return request('get', url, **kwargs)

  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 44, in request

    return session.request(method=method, url=url, **kwargs)

  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 383, in request

    resp = self.send(prep, **send_kwargs)

  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 486, in send

    r = adapter.send(request, **kwargs)

  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 378, in send

    raise ConnectionError(e)

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='itunes.apple.com', port=443): Max retries exceeded with url: /in/app/adobe-reader/id469337564?mt=8 (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)


皈依舞
浏览 1200回答 3
3回答

GCT1015

这里发生的事情是itunes服务器拒绝你的连接(你在短时间内从相同的ip地址发送了太多的请求)使用url超出最大重试次数:/ in / app / adobe-reader / id469337564?mt = 8错误跟踪是误导性的,它应该是“无法建立连接,因为目标机器主动拒绝它”。关于Github上的python.requests lib存在一个问题,请在此处查看要克服这个问题(不是一个问题,因为它会误导调试跟踪),你应该捕获与连接相关的异常,如下所示:try:&nbsp; &nbsp; page1 = requests.get(ap)except requests.exceptions.ConnectionError:&nbsp; &nbsp; r.status_code = "Connection refused"解决这个问题的另一种方法是,如果你使用足够的时间间隔向服务器发送请求,这可以通过sleep(timeinsec)python中的函数来实现(不要忘记导入睡眠)from time import sleep所有的请求都是很棒的python lib,希望能解决你的问题。

饮歌长啸

只需使用requests'功能:import requestsfrom requests.adapters import HTTPAdapterfrom requests.packages.urllib3.util.retry import Retrysession = requests.Session()retry = Retry(connect=3, backoff_factor=0.5)adapter = HTTPAdapter(max_retries=retry)session.mount('http://', adapter)session.mount('https://', adapter)session.get(url)这将GET是URL并在3的情况下重试3次requests.exceptions.ConnectionError。backoff_factor将有助于在定期请求配额的情况下在尝试避免再次失败之间应用延迟。看一看requests.packages.urllib3.util.retry.Retry,它有很多选项来简化重试。

吃鸡游戏

就这样做,粘贴以下代码代替page = requests.get(url):import timepage = ''while page == '':&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; page = requests.get(url)&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; print("Connection refused by the server..")&nbsp; &nbsp; &nbsp; &nbsp; print("Let me sleep for 5 seconds")&nbsp; &nbsp; &nbsp; &nbsp; print("ZZzzzz...")&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(5)&nbsp; &nbsp; &nbsp; &nbsp; print("Was a nice sleep, now let me continue...")&nbsp; &nbsp; &nbsp; &nbsp; continue别客气 :)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python