github模拟登陆
-
使用requests,xpath模拟GitHub登陆
-
具体步骤思路如下:
-
清空浏览器保存的GitHub网站的cookies
-
回到登陆界面登陆,F12开发者模式,分析参数
-
点击登陆,便可在开发者工具中看到各种请求,点开
-
可以看到请求地址为
https://github.com/session
,请求方式为POST -
分析提交表单,可以看到
authenticity_token、ga_id、timestamp、timestamp_secret
等参数无法自行构造 -
因此,重新清空浏览器cookies,返回到登陆界面,截获部分发送请求
-
- 查看login的源代码,全局搜索上述参数,因此知道
https://github.com/login
源代码中包含了上述参数。现在已经知道了所有信息的来源,接下来代码实现模拟登陆。
-
-
`
import requests from lxml import etree class Login(object): def __init__(self): self.headers={ "Referer":"https://github.com/", "Usser-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36", "Host":"github.com" } self.login_url="https://github.com/login" self.post_url="https://github.com/session" self.session=requests.Session() def token(self): response = self.session.get(self.login_url,headers=self.headers) selector=etree.HTML(response.text) li=selector.xpath("//div//input//@value") token=li[0] #authenticity_token timestamp=li[3] timestamp_secret=li[4] return [token,timestamp,timestamp_secret] def login(self,email,passwd): li=self.token() post_data={ "commit":"Sign in", "authenticity_token":li[0], "ga_id":"909892832.1595567052", "login":email, "password":passwd, "webauthn-support":"supported", "webauthn-iuvpaa-support":"suppotred", "return_to":"", "required_field_120e":"", "timestamp":li[1], "timestamp_secret":li[2] } response=self.session.post(self.post_url,data=post_data,headers=self.headers) if response.status_code==200: self.dynamics(response.text) #抓取public信息证明成功登陆 def dynamics(self,html): selector=etree.HTML(html) dynamics=selector.xpath("//div[@class='news']//ul/li[@class='public source ']//a/@href") print(dynamics) if __name__ == '__main__': login=Login() login.login(email="xxxxx",passwd="xxxxxxx")
-
综上实现GitHub模拟登陆
-
-