TypechoJoeTheme

Dcr163的博客

统计

使用python模拟登陆github网站

2021-11-13
/
0 评论
/
1,002 阅读
/
正在检测是否收录...
11/13

python登陆github网站

使用python模拟登陆Github网站,下面是代码要是没有 BeautifulSoup 解析库,请先安装。

安装命令:pip install beautifulsoup4

#!/usr/bin/python3
# -*- conding:utf-8 -*-
# 使用【Beautiful Soup】模拟登陆github网站
import requests
from bs4 import BeautifulSoup

if __name__ == '__main__':
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
    }
    # 构建一个请求会话
    with requests.session() as session:
        session.headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36'
        }
        # 先请求登陆页面
        url = 'https://github.com/login'
        res = session.get(url)
        bs = BeautifulSoup(res.text,'lxml')

        #登陆的post数据
        loginInfo = {
            'commit': 'Sign in',
            'webauthn-support': 'supported',
            'login':'username',     #github账号
            'password':'xxxx'         #github密码
        }
        # 获取登陆的参数 authenticity_token
        authInput = bs.find('input',attrs={'name':'authenticity_token'})
        loginInfo['authenticity_token'] = authInput.attrs['value']
        # 获取登陆的参数 timestamp_secret
        authInput = bs.find('input',attrs={'name':'timestamp_secret'})
        loginInfo['timestamp_secret'] = authInput.attrs['value']
        # 获取登陆的参数 timestamp
        authInput = bs.find('input',attrs={'name':'timestamp'})
        loginInfo['timestamp'] = authInput.attrs['value']
        # 提交登陆的地址
        loginUrl = 'https://github.com/session'
        # post提交登陆
        res2 = session.post(loginUrl,data=loginInfo)
        # 会员中心地址
        profileUrl = "https://github.com/settings/profile"
        # 获取会员中心地址
        res3 = session.get(profileUrl)
        res3H = BeautifulSoup(res3.text,'lxml')
        print(res3H.title.text)  #这行能打印出来 Your Profile 就算正常登陆成功了

python
朗读
赞(0)
版权属于:

Dcr163的博客

本文链接:

https://dcr163.cn/560.html(转载时请注明本文出处及文章链接)

评论 (0)

人生倒计时

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

最新回复

  1. William Tardent
    2024-02-27
  2. Maryann Hamer
    2024-02-27
  3. Sanora Pantano
    2024-02-24
  4. aa
    2024-02-21
  5. Kassie Baum
    2024-01-28

标签云