【发布时间】:2018-10-20 01:45:40
【问题描述】:
我一直在尝试抓取像 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}),结果相同。 @987654324 先@cookie 并在帖子中使用它们似乎也不起作用。
我怎样才能获得我的登录页面,最好不使用 Selenium?
【问题讨论】:
标签: python python-3.x web-scraping python-requests