python-Github 模拟登陆 获取登陆信息

import requests
from pyquery import PyQuery as pq


class Login(object):
    def __init__(self):
        self.headers = {
            'Referer': 'https://github.com/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
            'Host': 'github.com'
        }
        self.login_url = 'https://github.com/login'
        self.post_url = 'https://github.com/session'
        self.feed_url = 'https://github.com/dashboard-feed'
        self.logined_url = 'https://github.com/settings/profile'
        self.session = requests.Session()

    def get_authenticity_token(self):
        response = self.session.get(self.login_url, headers=self.headers)
        doc=pq(response.text)
        token=doc('input[name="authenticity_token"]').attr('value')
        return token

    def get_ga_id(self):
        response=self.session.get(self.login_url,headers=self.headers)
        doc=pq(response.text)
        ga_id=doc('input[class="js-octo-ga-id-input"]').attr('value')
        return ga_id

    def get_timestamp(self):
        response = self.session.get(self.login_url, headers=self.headers)
        doc = pq(response.text)
        timestamp=doc('input[name=timestamp]').attr('value')
        return timestamp

    def get_timestamp_secret(self):
        response = self.session.get(self.login_url, headers=self.headers)
        doc = pq(response.text)
        timestamp_secret=doc('input[name=timestamp_secret]').attr('value')
        return timestamp_secret

    def login(self, email, password):
        post_data = {
            'commit': 'Sign in',
            'utf8': '✓',
            'authenticity_token': self.get_authenticity_token(),
            'ga_id': self.get_ga_id(),
            'login': email,
            'password': password,
            'webauthn-support':'supported',
            'webauthn-iuvpaa-support':'supported',
            'timestamp':self.get_timestamp(),
            'timestamp_secret':self.get_timestamp_secret(),
        }
        response = self.session.post(self.post_url, data=post_data, headers=self.headers)
        response=self.session.get(self.feed_url,headers=self.headers)
        if response.status_code == 200:
            self.dynamics(response.text)

        response = self.session.get(self.logined_url, headers=self.headers)
        if response.status_code == 200:
            self.profile(response.text)

    def dynamics(self, html):
        doc = pq(html)
        dynamics = doc('div[class="d-flex flex-items-baseline"] div')
        for item in dynamics.items():
            dynamic = item.text().strip()   #删除开头结尾字符
            print(dynamic)
        dynamics = doc('div[class="d-flex flex-justify-between flex-items-baseline"] div')
        for item in  dynamics.items():
            dynamic = item.text().strip()    #删除开头结尾字符
            print(dynamic)

    def profile(self, html):
        doc=pq(html)
        name = doc('input[name="user[profile_name]"]').attr('value')
        print(name)


if __name__ == "__main__":
    login = Login()
    login.login(email='1179380391@qq.com', password='wjh07233')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值