Python请求GET需要很长时间才能响应某些请求 [英] Python requests GET takes a long time to respond to some requests

查看:127
本文介绍了Python请求GET需要很长时间才能响应某些请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python请求 get方法来查询

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,例如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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆