我一直在尝试创建一个像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})首先使用cookies并在文章中使用它们似乎也不起作用。
我怎样才能得到我的登录页面,最好不用Selenium?

最佳答案

这是因为表单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)

关于python - Python在发布时请求422错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50261869/

10-12 18:23