Python在帖子上请求422错误

时间:2018-05-09 20:52:37

标签: python python-3.x web-scraping python-requests

我一直在试图像GitHub这样需要登录身份验证的网站,但与Github不同,它没有和API一样。我遵循了these指令和许多其他指令,但似乎没有任何工作,只返回422错误。

from lxml import html

url = "https://github.com/login"
user = "my email"
pas = "associated password"

sess = requests.Session()
r = sess.get(url)

rhtml = html.fromstring(r.text)

#get all hidden input fields and make a dict of them
hidden = rhtml.xpath(r'//form//input[@type="hidden"]')
form = {x.attrib["name"]: x.attrib["value"] for x in hidden}

#add login creds to the dict
form['login'] = user
form['password'] = pas

#post
res = sess.post(url, data=form)

print(res)
# <Response [422]>

我也尝试了sess.post(url, data={'login':user, 'password':pas}),结果相同。 get首先使用cookie并在帖子中使用它们似乎也不起作用。

如何获取登录页面,最好不使用Selenium?

1 个答案:

答案 0 :(得分:2)

这是因为表格action与登录页面不同。

您可以使用requestsBeautifulSoup

来执行此操作
import requests
from bs4 import BeautifulSoup

url = "https://github.com/login"
user = "<username>"
pwd = "<password>"

with requests.Session() as s:

    r = s.get(url)
    soup = BeautifulSoup(r.content, "lxml")

    hidden = soup.find_all("input", {'type':'hidden'})
    target = "https://github.com" + soup.find("form")['action']
    payload = {x["name"]: x["value"] for x in hidden}

    #add login creds to the dict
    payload['login'] = user
    payload['password'] = pwd

    r = s.post(target, data=payload)
    print(r)