鱼C论坛

 找回密码
 立即注册
查看: 1880|回复: 7

[已解决]爬虫入门,模拟登录github时遇到了timestamp_secret怎么解呢?

[复制链接]
发表于 2021-8-31 23:16:23 | 显示全部楼层 |阅读模式

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

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

x
爬虫入门,模拟登录github时遇到了timestamp和timestamp_secret,其中 timestamp 靠着百度解决了,但是后面这个搜网页源码 ,连个毛不会找
最佳答案
2021-9-2 11:49:33
data不用考虑那么多参数:
  1. import requests
  2. import re


  3. def login():
  4.     session = requests.session()
  5.     session.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0'}
  6.     login_url = 'https://github.com/login'
  7.     r = session.get(login_url)
  8.     token = re.findall('name="authenticity_token" value="(.*?)"/>', r.text)[1]
  9.     print(token)
  10.     post_url = 'https://github.com/session'
  11.     data = {
  12.         'commit': 'Sign in',
  13.         'utf8': '✓',
  14.         'authenticity_token': token,
  15.         'login': 'email',
  16.         'password': 'password'
  17.     }
  18.     print(data)
  19.     session.post(post_url, data=data)
  20.     logined_url = 'https://github.com/settings/profile'
  21.     r = session.get(logined_url)
  22.     with open('github.html', 'w') as f:
  23.         f.write(r.text)


  24. if __name__ == '__main__':
  25.     login()
复制代码



别人的案例
  1. import requests
  2. from lxml import etree


  3. class Login(object):
  4.     def __init__(self):
  5.         self.headers = {
  6.             'Referer': 'https://github.com/',
  7.             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
  8.             'Host': 'github.com'
  9.         }
  10.         self.login_url = 'https://github.com/login'
  11.         self.post_url = 'https://github.com/session'
  12.         self.logined_url = 'https://github.com/settings/profile'
  13.         self.session = requests.Session()


  14.     def token(self):
  15.         response = self.session.get(self.login_url, headers=self.headers)
  16.         selector = etree.HTML(response.text)
  17.         token = selector.xpath('//input[1]/@value')[0]
  18.         return token

  19.     def login(self, email, password):
  20.         post_data = {
  21.             'commit': 'Sign in',
  22.             'utf8': '✓',
  23.             'authenticity_token': self.token(),
  24.             'login': email,
  25.             'password': password
  26.         }
  27.         response = self.session.post(self.post_url, data=post_data, headers=self.headers)
  28.         if response.status_code == 200:
  29.             self.dynamics(response.text)
  30.         response = self.session.get(self.logined_url, headers=self.headers)
  31.         if response.status_code == 200:
  32.             self.profile(response.text)

  33.     def dynamics(self, html):
  34.         selector = etree.HTML(html)
  35.         dynamics = selector.xpath('//div[contains(@class, "news")]//div[contains(@class, "alert")]')
  36.         for item in dynamics:
  37.             dynamic = ' '.join(item.xpath('.//div[@class="title"]//text()')).strip()
  38.             print(dynamic)

  39.     def profile(self, html):
  40.         selector = etree.HTML(html)
  41.         name = selector.xpath('//input[@id="user_profile_name"]/@value')[0]
  42.         email = selector.xpath('//select[@id="user_profile_email"]/option[2]/@value')[0]
  43.         # 能正确显示自己账号的信息即为成功
  44.         print(name, email)


  45. if __name__ == "__main__":
  46.     login = Login()
  47.     login.login(email='账号', password='密码')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-1 07:29:20 From FishC Mobile | 显示全部楼层
本帖最后由 suchocolate 于 2021-9-1 07:33 编辑

贴一下你的代码。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-2 00:04:13 | 显示全部楼层
suchocolate 发表于 2021-9-1 07:29
贴一下你的代码。
  1. import requests
  2. import re  #正则模块
  3. import time
  4. # 输入毫秒级的时间,转出正常格式的时间
  5. def timeStamp(timeNum):
  6.     global otherStyleTime
  7.     timeStamp = float(timeNum / 1000)
  8.     timeArray = time.localtime(timeStamp)
  9.     otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
  10.     return otherStyleTime

  11. def login():
  12.     #session
  13.     session = requests.session()
  14.     #headers
  15.     session.headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0'}
  16.     #url1  1.获取token  2. 发请求 3. 正则提取
  17.     url1='https://github.com/login'
  18.     response1=session.get(url1).content.decode()  #正则只对str操作,所以要转换
  19.     #name="authenticity_token" value="VMAW29pzDbiHSBXuhaR0BhMPyPatsl/3FbZ8ig2MtiS2SXMw
  20.     token=re.findall('name="authenticity_token" value="(.*?)"/>',response1)[1]  #.匹配多个*多个?非贪婪模式
  21.     print(token)    #10054, '远程主机强迫关闭了一个现有的连接
  22.     #url2  构建表单数据
  23.     url2 ='https://github.com/session'
  24.     data={'commit':'Sign in',
  25.           'authenticity_token':'VMAW29pzDbiHSBXuhaR0BhMPyPatsl/3FbZ8ig2MtiS2SXMwOoKS2baWSNwEtX1qUcX7aVWo97N17Cz+8W21og==',
  26.           'login':'',
  27.           'password':'',
  28.           'trusted_device':'',
  29.           'webauthn-support':'supported',
  30.           'webauthn-iuvpaa-support':'unsupported',
  31.           'return_to':'https://github.com/login',
  32.           'allow_signup':'',
  33.           'client_id':'',
  34.           'integration':'',
  35.           'required_field_6ab1':'',
  36.           'timestamp':otherStyleTime,
  37.           'timestamp_secret':'[color=Red]cf8f6694c191d005c8b9171b29b9d8d4fad8f539801d0feb18417263f507fe42[/color]'}   #这玩意儿怎么搞?
  38.     print(data)
  39.     #请求登录
  40.     session.post(url2,data=data)
  41.     #验证
  42.     url3='https://github.com/1667334486@qq.com'
  43.     response=session.get(url3)
  44.     with open('github.html','wb')as f:
  45.         f.write(response.content)

  46. if __name__ == '__main__':
  47.     login()

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-2 11:49:33 | 显示全部楼层    本楼为最佳答案   
data不用考虑那么多参数:
  1. import requests
  2. import re


  3. def login():
  4.     session = requests.session()
  5.     session.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0'}
  6.     login_url = 'https://github.com/login'
  7.     r = session.get(login_url)
  8.     token = re.findall('name="authenticity_token" value="(.*?)"/>', r.text)[1]
  9.     print(token)
  10.     post_url = 'https://github.com/session'
  11.     data = {
  12.         'commit': 'Sign in',
  13.         'utf8': '✓',
  14.         'authenticity_token': token,
  15.         'login': 'email',
  16.         'password': 'password'
  17.     }
  18.     print(data)
  19.     session.post(post_url, data=data)
  20.     logined_url = 'https://github.com/settings/profile'
  21.     r = session.get(logined_url)
  22.     with open('github.html', 'w') as f:
  23.         f.write(r.text)


  24. if __name__ == '__main__':
  25.     login()
复制代码



别人的案例
  1. import requests
  2. from lxml import etree


  3. class Login(object):
  4.     def __init__(self):
  5.         self.headers = {
  6.             'Referer': 'https://github.com/',
  7.             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
  8.             'Host': 'github.com'
  9.         }
  10.         self.login_url = 'https://github.com/login'
  11.         self.post_url = 'https://github.com/session'
  12.         self.logined_url = 'https://github.com/settings/profile'
  13.         self.session = requests.Session()


  14.     def token(self):
  15.         response = self.session.get(self.login_url, headers=self.headers)
  16.         selector = etree.HTML(response.text)
  17.         token = selector.xpath('//input[1]/@value')[0]
  18.         return token

  19.     def login(self, email, password):
  20.         post_data = {
  21.             'commit': 'Sign in',
  22.             'utf8': '✓',
  23.             'authenticity_token': self.token(),
  24.             'login': email,
  25.             'password': password
  26.         }
  27.         response = self.session.post(self.post_url, data=post_data, headers=self.headers)
  28.         if response.status_code == 200:
  29.             self.dynamics(response.text)
  30.         response = self.session.get(self.logined_url, headers=self.headers)
  31.         if response.status_code == 200:
  32.             self.profile(response.text)

  33.     def dynamics(self, html):
  34.         selector = etree.HTML(html)
  35.         dynamics = selector.xpath('//div[contains(@class, "news")]//div[contains(@class, "alert")]')
  36.         for item in dynamics:
  37.             dynamic = ' '.join(item.xpath('.//div[@class="title"]//text()')).strip()
  38.             print(dynamic)

  39.     def profile(self, html):
  40.         selector = etree.HTML(html)
  41.         name = selector.xpath('//input[@id="user_profile_name"]/@value')[0]
  42.         email = selector.xpath('//select[@id="user_profile_email"]/option[2]/@value')[0]
  43.         # 能正确显示自己账号的信息即为成功
  44.         print(name, email)


  45. if __name__ == "__main__":
  46.     login = Login()
  47.     login.login(email='账号', password='密码')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-2 23:26:47 | 显示全部楼层
suchocolate 发表于 2021-9-2 11:49
data不用考虑那么多参数:

刚开始  只是说 要 写刷新后变化的项
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-3 00:56:32 | 显示全部楼层
suchocolate 发表于 2021-9-1 07:29
贴一下你的代码。

远程主机强迫关闭了一个现有的连接  这种有没有好的避免方法呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-3 09:57:11 | 显示全部楼层
我爱l两条柴 发表于 2021-9-3 00:56
远程主机强迫关闭了一个现有的连接  这种有没有好的避免方法呢?

操作太快了,time.sleep一会,或重启py
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-7 20:17:10 | 显示全部楼层
爬虫入门,模拟登录github时遇到了timestamp和timestamp_secret,哈哈哈哈我遇到了同样的问题,然后搜索到了你的问题。握个爪!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-14 03:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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