超过URL的最大重试次数

我试图得到这个url的内容“ https://itunes.apple.com/in/genre/ios-business/id6000?mt=8 ”,并显示此错误

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) 

代码是

 url="https://itunes.apple.com/in/genre/ios-business/id6000?mt=8" page = requests.get(url) 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) 

当我尝试(0,2)的范围它的工作原理,但是当我把范围在100的它显示这个错误。

这里发生的事情是, itunes服务器拒绝你的连接(你在短时间内发送同样的IP地址太多的请求)

最大重试次数超过了url:/ in / app / adobe-reader / id469337564?mt = 8

错误跟踪误导它应该是“因为目标机器主动拒绝而不能build立连接”

在Github上关于python.requests lib有一个问题,在这里检查一下

为了克服这个问题(不是一个问题,因为它是错误的debugging跟踪),你应该捕捉连接相关的exception,如下所示:

 try: page1 = requests.get(ap) except requests.exceptions.ConnectionError: r.status_code = "Connection refused" 

解决这个问题的另一种方法是,如果使用足够的时间间隔向服务器发送请求,可以通过python中的sleep(timeinsec)函数来实现(不要忘记导入睡眠)

 from time import sleep 

总之所有的要求是非常棒的Python库,希望能够解决你的问题。

只要做到这一点,

将以下代码粘贴到page = requests.get(url)

 import time page = '' while page == '': try: page = requests.get(url) except: print("Connection refused by the server..") print("Let me sleep for 5 seconds") print("ZZzzzz...") time.sleep(5) print("Was a nice sleep, now let me continue...") continue 

别客气 :)

pip install pyopenssl似乎为我解决它。

https://github.com/requests/requests/issues/4246