请求中的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)

当我用(0,2)尝试range时,它可以工作,但当我把range放在100s中时,它显示了这个错误:

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)
872582 次浏览

这里发生的是itunes服务器拒绝你的连接(你在短时间内从同一个ip地址发送了太多的请求)

url: /in/app/adobe-reader/id469337564?太= 8

错误跟踪是误导性的,它应该是类似“无法建立连接,因为目标机器主动拒绝它”;的东西。

关于python有一个问题。请求库在Github,检查它在这里

为了克服这个问题(与其说是一个问题,不如说是误导调试跟踪),你应该像这样捕捉连接相关的异常:

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

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

from time import sleep

所有的请求都是很棒的python库,希望能解决你的问题。

就这么做,

将以下代码粘贴在page = requests.get(url)的位置:

import time


page = ''
while page == '':
try:
page = requests.get(url)
break
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

只需使用requests特性:

import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry




session = 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,并在requests.exceptions.ConnectionError的情况下重试3次。backoff_factor将有助于在尝试之间应用延迟,以避免在周期性请求配额的情况下再次失败。

看一下urllib3.util.retry.Retry,它有很多选项来简化重试。

实现异常处理总是好的。它不仅有助于避免脚本意外退出,还有助于记录错误和信息通知。当使用Python请求时,我更喜欢捕获这样的异常:

    try:
res = requests.get(adress,timeout=30)
except requests.ConnectionError as e:
print("OOPS!! Connection Error. Make sure you are connected to Internet. Technical Details given below.\n")
print(str(e))
renewIPadress()
continue
except requests.Timeout as e:
print("OOPS!! Timeout Error")
print(str(e))
renewIPadress()
continue
except requests.RequestException as e:
print("OOPS!! General Error")
print(str(e))
renewIPadress()
continue
except KeyboardInterrupt:
print("Someone closed the program")

这里的renewIPadress()是一个用户定义函数,它可以在IP地址被阻塞时更改IP地址。你可以不用这个函数。

我有类似的问题,但下面的代码为我工作。

url = <some REST url>
page = requests.get(url, verify=False)

verify=False禁用SSL验证。Try和catch可以像往常一样添加。

为这个请求添加报头。

headers={
'Referer': 'https://itunes.apple.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
}


requests.get(ap, headers=headers)

当我在写一个selenium浏览器测试脚本时,我在使用JS api调用之前调用driver.quit()时遇到了这个错误。记住,放弃网络驱动是最不应该做的事情!

补充我自己的经验,为那些在未来正在经历这种情况的人。我的具体错误是

Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'

事实证明,这实际上是因为我已经达到了系统上打开的文件的最大数量。它与失败的连接无关,甚至与所指出的DNS错误无关。

即使在安装pyopenssl和尝试各种python版本后,我也无法在Windows上工作(而它在mac上工作得很好),所以我切换到urllib,它可以在python 3.6(从python .org)和3.7 (anaconda)上工作

import urllib
from urllib.request import urlopen
html = urlopen("http://pythonscraping.com/pages/page1.html")
contents = html.read()
print(contents)

在公司环境中指定代理为我解决了这个问题。

page = requests.get("http://www.google.com:80", proxies={"http": "http://111.233.225.166:1234"})

完整的错误是:

requests.exceptions.ConnectionError: httpconnectionpool (host='www.google.com', port=80): Max retries exceeded with url: /(由NewConnectionError(': Failed to establish a new connection: [WinError 10060]连接尝试失败,因为被连接的一方在一段时间后没有正确响应,或已建立的连接失败,因为连接的主机未能响应'))

加上我自己的经验:

r = requests.get(download_url)

当我试图下载url中指定的文件时。

错误在于

HTTPSConnectionPool(host, port=443): Max retries exceeded with url (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))

我通过在函数中添加verify = False来纠正它,如下所示:

r = requests.get(download_url + filename)
open(filename, 'wb').write(r.content)

检查网络连接。我有这个,虚拟机没有一个正确的网络连接。

当我在浏览器中运行路线时,我有同样的错误,但在邮差中,它工作得很好。我的问题是,在查询字符串之前的路由之后没有/

127.0.0.1:5000/api/v1/search/?location=Madina引发错误并在search为我工作后删除/

< p > import time 并添加:

time.sleep(6)

在for循环的某处,以避免在短时间内向服务器发送过多的请求。 数字6意味着:6秒。 继续从1开始测试数字,直到达到有助于避免问题的最小秒数

也可能是网络配置的问题。因此,为此你需要重新配置你的网络配置。

Ubuntu: Sudo vim /etc/network/interfaces

在dns-nameserver中添加“8.8.8.8”并保存。

重置你的网络:/etc/init.d /网络重启

现在试试. .

当你向https://itunes.apple.com的公共IP地址发送太多请求时,就会发生这种情况。正如你所看到的,这是由于某些原因不允许/阻止访问与https://itunes.apple.com映射的公共IP地址造成的。一个更好的解决方案是下面的python脚本,它计算任何域的公共IP地址,并创建到/etc/hosts文件的映射。

import re
import socket
import subprocess
from typing import Tuple


ENDPOINT = 'https://anydomainname.example.com/'
ENDPOINT = 'https://itunes.apple.com/'


def get_public_ip() -> Tuple[str, str, str]:
"""
Command to get public_ip address of host machine and endpoint domain
Returns
-------
my_public_ip : str
Ip address string of host machine.
end_point_ip_address : str
Ip address of endpoint domain host.
end_point_domain : str
domain name of endpoint.


"""
# bash_command = """host myip.opendns.com resolver1.opendns.com | \
#     grep "myip.opendns.com has" | awk '{print $4}'"""
# bash_command = """curl ifconfig.co"""
# bash_command = """curl ifconfig.me"""
bash_command = """ curl icanhazip.com"""
my_public_ip = subprocess.getoutput(bash_command)
my_public_ip = re.compile("[0-9.]{4,}").findall(my_public_ip)[0]
end_point_domain = (
ENDPOINT.replace("https://", "")
.replace("http://", "")
.replace("/", "")
)
end_point_ip_address = socket.gethostbyname(end_point_domain)
return my_public_ip, end_point_ip_address, end_point_domain




def set_etc_host(ip_address: str, domain: str) -> str:
"""
A function to write mapping of ip_address and domain name in /etc/hosts.
Ref: https://stackoverflow.com/questions/38302867/how-to-update-etc-hosts-file-in-docker-image-during-docker-build


Parameters
----------
ip_address : str
IP address of the domain.
domain : str
domain name of endpoint.


Returns
-------
str
Message to identify success or failure of the operation.


"""
bash_command = """echo "{}    {}" >> /etc/hosts""".format(ip_address, domain)
output = subprocess.getoutput(bash_command)
return output




if __name__ == "__main__":
my_public_ip, end_point_ip_address, end_point_domain = get_public_ip()
output = set_etc_host(ip_address=end_point_ip_address, domain=end_point_domain)
print("My public IP address:", my_public_ip)
print("ENDPOINT public IP address:", end_point_ip_address)
print("ENDPOINT Domain Name:", end_point_domain )
print("Command output:", output)

你可以在运行你想要的函数之前调用上面的脚本:)

我的情况比较特殊。我试了上面的答案,没有一个管用。我突然想,是不是和我的网络代理有关?你知道,我在中国大陆,如果没有代理,我无法访问像谷歌这样的网站。然后我关掉了网络代理,问题就解决了。

我正在用Gauge编写一个测试,我也遇到了这个错误,这是因为我试图在没有激活VPN的情况下请求内部URL。

在我的例子中,我在python脚本中部署了一些docker容器,然后调用其中一个部署的服务。当我在调用服务之前添加一些延迟时,错误被修复。我认为它需要时间来准备接受连接。

from time import sleep
#deploy containers
#get URL of the container
sleep(5)
response = requests.get(url,verify=False)
print(response.json())

首先我运行run.py文件,然后我运行unit_test.py文件,它为我工作