Python實(shí)戰(zhàn)案例,requests模塊,Python模擬登陸GitHub并獲取信息
這里先對(duì)GitHub進(jìn)行模擬登陸,了解會(huì)話及Cookies相關(guān)知識(shí)。
/ 01 / 網(wǎng)頁(yè)分析
首先看一下登錄頁(yè),獲取authenticity_token參數(shù)值,是一個(gè)隱藏式表單元素。
查看登錄頁(yè)的Response Headers,這里的Set-Cookie字段,是設(shè)置Cookies的過(guò)程。這邊呢我的理解是,當(dāng)你在session那個(gè)網(wǎng)頁(yè)填寫賬號(hào)和密碼后,這里就會(huì)自動(dòng)生成一個(gè)Cookies返回(其實(shí)我也很暈...)。
將Preserve Log打開(kāi)(表示顯示持續(xù)日志),然后輸入賬號(hào)及密碼,找到session這個(gè)請(qǐng)求。最后得知請(qǐng)求的網(wǎng)址及請(qǐng)求方式(POST)。
這里是session的請(qǐng)求頭和表單信息,能看到生成的Cookies信息。
獲取用戶動(dòng)態(tài)及個(gè)人信息的網(wǎng)頁(yè)就不說(shuō)了,簡(jiǎn)單操作。
需要提的一點(diǎn)就是用戶動(dòng)態(tài)的網(wǎng)頁(yè)是Ajax請(qǐng)求,崔大的代碼過(guò)時(shí)了呀...
Python騰訊大牛直播預(yù)約:

/ 02 / 數(shù)據(jù)獲取
實(shí)現(xiàn)代碼如下所示。
from lxml import etree
import requests
class Login(object):
# Login類繼承object對(duì)象,高級(jí)特性
def __init__(self):
# 特殊的方法,類的構(gòu)造函數(shù)或初始化方法,當(dāng)創(chuàng)建了Login類的實(shí)例時(shí)就會(huì)調(diào)用該方法
# self代表類的實(shí)例,self在定義類的方法時(shí)是必須有的
self.headers = {
'Referer': 'https: // github.com /',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; 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'
# 這里因?yàn)榇薮蟮拇a不行了,自己去找用戶動(dòng)態(tài)信息的真正請(qǐng)求,又是Ajax
self.get_users_url = 'https://github.com/dashboard-feed'
self.post_url = 'https://github.com/session'
self.logined_url = 'https://github.com/settings/profile'
# 創(chuàng)建一個(gè)session對(duì)象
self.session = requests.Session()
# 類的方法與普通的函數(shù)只有一個(gè)特別的區(qū)別, 它們必須有一個(gè)額外的第一個(gè)參數(shù)名稱, 按照慣例它的名稱是self
# self不是python關(guān)鍵字,我們把它換成cool也是可以正常執(zhí)行的
def token(self):
# 獲取authenticity_token參數(shù)值
response = self.session.get(self.login_url, headers=self.headers)
result = etree.HTML(response.text)
token = result.xpath('//div//input[2]/@value')[0]
return token
def login(self, email, password):
# 模擬登陸GitHub,POST請(qǐng)求
post_data = {
'commit': 'Sign in',
'utf8': '?',
'authenticity_token': self.token(),
'login': email,
'password': password
}
response = self.session.post(self.post_url, data=post_data, headers=self.headers)
# 獲取我所關(guān)注的人的動(dòng)態(tài)
if response.status_code == 200:
response = self.session.get(self.get_users_url, headers=self.headers)
self.dynamics(response.text)
# 獲取我的個(gè)人信息
response = self.session.get(self.logined_url, headers=self.headers)
if response.status_code == 200:
self.profile(response.text)
def dynamics(self, html):
# 獲取我所關(guān)注的人的動(dòng)態(tài)
result = etree.HTML(html)
dynamics = result.xpath('//div[@class="d-flex flex-items-baseline"]/div')
for item in dynamics:
# 這里我是直接獲取標(biāo)簽所有文字,然后去除換行及空格
print(item.xpath('string(.)').strip().replace('\n', '').replace(' ', ' ').replace(' ', ' ').replace(' ', ' ').replace(' ', ' '))
def profile(self, html):
# 獲取我的個(gè)人信息
result = etree.HTML(html)
name = result.xpath('//input[@name="user[profile_name]"]/@value')[0]
email = result.xpath('//select[@name="user[profile_email]"]/option[@selected="selected"]/text()')
print(name, email)
if __name__ == '__main__':
# 類的實(shí)例化類似函數(shù)調(diào)用方式
login = Login()
# 使用點(diǎn)號(hào) . 來(lái)訪問(wèn)對(duì)象的屬性
login.login(email='你的賬號(hào)', password='你的密碼')
最后成功登陸,獲取動(dòng)態(tài)及個(gè)人信息。
工程師必備
- 項(xiàng)目客服
- 培訓(xùn)客服
- 平臺(tái)客服
TOP




















