请求模块中 url 的 HTTP 连接池错误(超出最大重试次数)

HTTP Connection Pool error (Max retries exceeded) with url in requests module

我尝试通过 python 中的请求模块登录网站。这是我的 python 代码:

import requests

url='http://example.com/login.php' 
Data={'user':'user_name','pass':'my_password'} 
session=requests.session() 
req=session.post(url=URL, data=Data)

在 运行 之后,出现以下错误:

HTTPConnectionPool(host='example.com', port=80): Max retries exceeded with url: /login.php (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)

如何修复此错误?

Python 变量区分大小写。您的变量是小写 url 但在 session.post 方法中您将 url 参数设置为大写 URL。这可能是个问题。

我遇到了这个问题。在我的例子中,URL 是错误的,像这样:

API = 'http://http://localhost:54057/api'

据我了解,是代理造成了障碍。 您可以通过在传递 url.

时添加代理作为参数来实现
import requests
site = "yourscrappingsite.com"
proxies = {"https": "http://yourprox.com:PortNo"}
page = requests.get(url=site,proxies=proxies)

这应该适合你。让我知道您的反馈。