鱼C论坛

 找回密码
 立即注册
查看: 1220|回复: 0

[技术交流] 模拟登陆github

[复制链接]
发表于 2018-2-26 17:34:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. from lxml import etree
  3. class Spider:
  4.     def __init__(self):
  5.         self.url = 'https://github.com/'
  6.         self.loginurl = 'https://github.com/login'
  7.         self.posturl = 'https://github.com/session'
  8.         self.session = requests.session()
  9.         self.headers = {
  10.             "Connection":"keep-alive",
  11.             "Host":"github.com",
  12.             "Referer":"https://www.jianshu.com/p/1f8a45b3f20a",
  13.             "Upgrade-Insecure-Requests":"1",
  14.             "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36"
  15.             }
  16.     def get_token(self):
  17.         html = self.session.get(self.loginurl,headers = self.headers).text
  18.         content = etree.HTML(html)
  19.         token = content.xpath('//input[2]/@value')[0]
  20.         return token
  21.     def post(self):
  22.         login = input('请输入您的帐号:')
  23.         password = input('请输入您的密码:')
  24.         form_data = {
  25.             "commit":"签到",
  26.             "utf8":"✓",
  27.             "authenticity_token":self.get_token(),
  28.             "login":login,
  29.             "password":password
  30.             }
  31.         self.session.post(self.posturl,data = form_data,headers = self.headers)
  32.     def verification(self):
  33.         content = self.session.get(self.url,headers = self.headers).text
  34.         html = etree.HTML(content)
  35.         result = html.xpath('//img/@alt')[0]
  36.         print(result)
  37.     def main(self):
  38.         self.post()
  39.         self.verification()
  40. if __name__ == '__main__':
  41.     spider = Spider()
  42.     spider.main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 03:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表