Python请求GET需要很长时间才能响应一些请求 [英] Python requests GET takes a long time to respond to some requests
问题描述
我正在使用 Python 请求 get 方法来查询 MediaWiki API,但是需要很长时间才能收到响应.相同的请求通过 Web 浏览器非常快速地收到响应.我在请求 google.com 时遇到了同样的问题.以下是我在 Windows 10 上的 Python 3.5 中尝试的示例代码:
I am using Python requests get method to query the MediaWiki API, but it takes a lot of time to receive the response. The same requests receive the response very fast through a web browser. I have the same issue requesting google.com. Here are the sample codes that I am trying in Python 3.5 on Windows 10:
response = requests.get("https://www.google.com")
response = requests.get("https://en.wikipedia.org/wiki/Main_Page")
response = requests.get("http://en.wikipedia.org/w/api.php?", params={'action':'query', 'format':'json', 'titles':'Labor_mobility'})
但是,我在检索其他网站时不会遇到这个问题,例如:
However, I don't face this issue retrieving other websites like:
response = requests.get("http://www.stackoverflow.com")
response = requests.get("https://www.python.org/")
推荐答案
这听起来像是与服务器的底层连接存在问题,因为对其他 URL 的请求有效.想到这些:
This sounds like there is an issue with the underlying connection to the server, because requests to other URLs work. These come to mind:
- 服务器可能只允许特定的用户代理字符串
尝试添加无害的标头,例如:requests.get("https://www.example.com", headers={"User-Agent": "Mozilla/5.0 (X11; CrOS x86_6412871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36"})
Try adding innocuous headers, e.g.: requests.get("https://www.example.com", headers={"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36"})
- 服务器限制了你
等待几分钟,然后重试.如果这解决了您的问题,您可以通过添加 time.sleep()
来减慢代码速度,以防止再次受到速率限制.
Wait for a few minutes, then try again. If this solves your issue, you could slow down your code by adding time.sleep()
to prevent being rate-limited again.
- IPv6 不起作用,但 IPv4 起作用
通过执行 curl --ipv6 -v https://www.example.com
进行验证.然后,与 curl --ipv4 -v https://www.example.com
进行比较.如果后者明显更快,则您的 IPv6 连接可能有问题.在此处查看可能的解决方案.
Verify by executing curl --ipv6 -v https://www.example.com
. Then, compare to curl --ipv4 -v https://www.example.com
. If the latter is significantly faster, you might have a problem with your IPv6 connection. Check here for possible solutions.
如果这不能解决您的问题,我在此处收集了一些其他可能的解决方案.
If that did not solve your issue, I have collected some other possible solutions here.
这篇关于Python请求GET需要很长时间才能响应一些请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!