zoukankan      html  css  js  c++  java
  • 携带cookies请求github个人信息(类封装)

    本文请求的是gitbub个人设置页 的邮箱信息,可以在函数内添加您的需求,代码内已注明

    # -*- coding: utf-8 -*-
    # @Time    : 2018/12/2620:48
    # @Auther  : zhangxinxin
    # @Email   : 778786617@qq.com
    # @Software: PyCharm
    
    # 需要登陆的情况
    """
    场景:个人信息页, 订单也页, 需要登陆权限后才可以访问
    权限验证: 网站通过token 或 session id 来限制访问页面
    sessionid: http无状态,
    """
    import requests
    from lxml import etree
    
    
    class GitHub(object):
        def __init__(self):
            self.profile_url = 'https://github.com/settings/profile'
            self.login_url = "https://github.com/login"
            self.do_login_url = "https://github.com/session"
            self.login_html = ''
            self.profile_dom = ''
            self.s = requests.Session()
            self.authenticity_token = ''
            self.headers = {
                'Host': 'github.com',
                'Referer': 'https://github.com/login',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
            }
            self.parse_login_html()
            self.session_args = {
                'commit': 'Sign in',
                'utf8': '✓',
                'authenticity_token': self.authenticity_token,
                'login': '您的账号',
                'password': '您的密码'
            }
    
        def get_login_html(self):
            while True:
                try:
                    login_html = self.s.get(self.login_url, headers=self.headers).text
                    self.login_html = etree.HTML(login_html)
                except Exception as e:
                    print(e)
                    print('请求失败')
                else: break
    
        def parse_login_html(self):
            self.get_login_html()
            self.authenticity_token = self.login_html.xpath('//input[@name="authenticity_token"]/@value')[0]
            print('token:', self.authenticity_token)
    
        def get_session(self):
            while True:
                try:
                    session_resp = self.s.post(self.do_login_url, headers=self.headers, data=self.session_args)
                    print(session_resp)
                except Exception as e:
                    print(e)
                    break
                else: break
    
        def profile_html(self):
            """可在此函数内添加您的需求"""
            profile_resp = self.s.get(self.profile_url, headers=self.headers)
            if profile_resp.status_code != requests.codes.ok:
                raise Exception("请求个人页设置失败")
            # print(profile_resp.text)
            self.profile_dom = etree.HTML(profile_resp.text)
            profile_email = self.profile_dom.xpath('//select[@id="user_profile_email"]/option[2]/text()')[0]
            print(profile_email)
    
        def run(self):
            self.get_session()
            self.profile_html()
    
    
    if __name__ == '__main__':
        A = GitHub()
        A.run()
    
    

    注 : 1. 作者github主页https://github.com/18839739027

    2. 仅可用于学习交流,谢谢

  • 相关阅读:
    SSE特殊指令集系列之二字节绝对差值求和指令
    HDR阴影高光图像增强
    移植FFMPEG到VS2008系列之二
    SSE2指令集系列之一浮点运算指令
    移植FFMPEG到VS2008系列之三
    网站添加时间线
    HTML实体符号代码速查表
    div显示在object、embed之上~
    不用JS照样使IE6支持PNG 24位背景图支持透明背景且链接不会失去焦点
    CSS 针对谷歌浏览器(Chrome) safari的webkit核心浏览器CSS hack
  • 原文地址:https://www.cnblogs.com/UTF-8-xinxin/p/10240949.html
Copyright © 2011-2022 走看看