构造表单模拟登陆GitHub

1、输入登陆网址:url = https://github.com/login?return_to=%2Fjoin

输入注册过的账号密码,登陆成功之后,查看post请求的session,查看需要提交的formdata表单,然后构造字典数据。


2、观察发现除了authenticity_token之外,别的可以直接构造字典,authenticity_token这个比较复杂,查看源代码发现在一个hidden的元素里,用XPath提取即可。所以初始字典构造为除authenticity_token之外的其余的几个键值对。


3、获取初始的网页源代码,来获取authenticity_token的值。


4、提取之后,要把token数据添加到formData里。

formData['authenticity_token'] = token

5、使用requests的post方法提交即可,注意post的网址是session的request url



6、完整代码如下:

import requests
import urllib.request
from lxml import etree
loginUrl = 'https://github.com/login?return_to=%2Fjoin'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36'
                         ' (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'}
page = urllib.request.urlopen(loginUrl).read().decode('utf-8')
s = etree.HTML(page)
token = s.xpath('//*[@id="login"]/form/input[2]/@value')[0]
# print(token)
formData = {
    'Referer': 'https://github.com/session',
    'commit': 'Sign in',
    'login': 'xxxx',
    'password': 'xxxx',
    'utf8': ''
}
formData['authenticity_token'] = token
session_url = 'https://github.com/session'
r = requests.post(session_url, data=formData, headers=headers)
html = r.text

if r.url == 'https://github.com/session':
    print('Login successfully!!!')
else:
    print("failed!")

猜你喜欢

转载自blog.csdn.net/qq_29541277/article/details/80049064