Python请求ConnectionError

时间:2017-02-03 20:46:14

标签: python login python-requests

我正在尝试登录此website。使用以下代码:

import requests

values = {'password': 'somepass', 'username': 'someusername'}
login_url = "http://voobly.com/login"

session_requests = requests.session()
r = session_requests.post(login_url, data=values,
                          headers=dict(referer=login_url))

这是登录网站的表格。

<form action="/login/auth" method="post">
    <label for="username">Username: </label>
    <input type="text" name="username" id="username" class="inputfield" value="">
    <label for="password">Password: </label>
    <input type="password" name="password" id="password" class="inputfield">
    <input type="submit" value="Login" class="login-button">
</form>

好吧,当我运行代码时,我收到了这个错误:

HTTPConnectionPool(host='www.voobly.com', port=80): Max retries exceeded with url: /login
(Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f7cca037be0>: 
Failed to establish a new connection: [Errno -2] Name or service not known',))

我怎么解决这个问题?我可以在浏览器中登录,但我无法通过python代码登录。

1 个答案:

答案 0 :(得分:1)

您正在向错误的网址发送POST请求,该请求应为http://www.voobly.com/login/auth

enter image description here

import requests

values = {'password': 'somepass', 'username': 'someusername'}
login_url = "http://www.voobly.com/login/auth"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64)', 
           ... # more...
           'Referer': 'http://www.voobly.com/login'}

session_requests = requests.session()
r = session_requests.post(login_url, data=values,
                          headers=headers)