本文請求的是gitbub我的設置頁 的郵箱信息,能夠在函數內添加您的需求,代碼內已註明html
# -*- 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/18839739027python
2. 僅可用於學習交流,謝謝git