• 模拟登陆github


    import requests
    from lxml import etree
    
    
    class Login(object):
        def __init__(self):
            self.headers = {
                'Referer': 'https://github.com/',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                'Host': 'github.com'
            }
            self.login_url = 'https://github.com/login'
            self.post_url = 'https://github.com/session'
            self.logined_url = 'https://github.com/settings/profile'
            self.session = requests.Session()  #方便请求头cookie的处理
        
        def token(self):
            response = self.session.get(self.login_url, headers=self.headers)
            selector = etree.HTML(response.text)
            token = selector.xpath('//div//input[2]/@value')
            return token
        
        def login(self, email, password):
            post_data = {
                'commit': 'Sign in',
                'utf8': '✓',
                'authenticity_token': self.token()[0],
                'login': email,
                'password': password
            }
            response = self.session.post(self.post_url, data=post_data, 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):
            selector = etree.HTML(html)
            dynamics = selector.xpath('//div[contains(@class, "news")]//div[contains(@class, "alert")]')
            for item in dynamics:
                dynamic = ' '.join(item.xpath('.//div[@class="title"]//text()')).strip()
                print(dynamic)
        
        def profile(self, html):
            selector = etree.HTML(html)
            name = selector.xpath('//input[@id="user_profile_name"]/@value')[0]
            email = selector.xpath('//select[@id="user_profile_email"]/option[@value!=""]/text()')
            print(name, email)
    
    
    if __name__ == "__main__":
        login = Login()
        login.login(email='cqc@cuiqingcai.com', password='password')
    

      

  • 相关阅读:
    [LintCode] 1563. Shortest path to the destination
    [LintCode] 1835. Number of Ways to Stay in the Same Place After Some Steps I
    [Algo] 140. Maximum Path Sum Binary Tree III
    [Algo] 141. Binary Tree Path Sum To Target III
    [LintCode] 597. Subtree with Maximum Average
    [LintCode] 596. Minimum Subtree
    [LC] 16. 3Sum Closest
    [Algo] 182. 2 Sum All Pair II
    [Algo] 181. 2 Sum All Pair I
    创建ORACLE 查询用户
  • 原文地址:https://www.cnblogs.com/lxx7/p/10679731.html
Copyright © 2020-2023  润新知