利用Python實現(xiàn)模擬登錄知乎
環(huán)境與開發(fā)工具
在抓包的時候,開始使用的是Chrome開發(fā)工具中的Network,結(jié)果沒有抓到,后來使用Fiddler成功抓取數(shù)據(jù)。下面逐步來細(xì)化上述過程。
模擬知乎登錄前,先看看本次案例使用的環(huán)境及其工具:
- Windows 7 + Python 2.75
- Chrome + Fiddler: 用來監(jiān)控客戶端與服務(wù)器的通訊情況,以及查找相關(guān)參數(shù)的位置。
模擬過程概述
- 使用Google瀏覽器結(jié)合Fiddler來監(jiān)控客戶端與服務(wù)端的通訊過程;
- 根據(jù)監(jiān)控結(jié)果,構(gòu)造請求服務(wù)器過程中傳遞的參數(shù);
- 使用Python模擬參數(shù)傳遞過程。
客戶端與服務(wù)端通信過程的幾個關(guān)鍵點:
- 登錄時的url地址。
- 登錄時提交的參數(shù)【params】,獲取方式主要有兩種:第一、分析頁面源代碼,找到表單標(biāo)簽及屬性。適應(yīng)比較簡單的頁面。第二、使用抓包工具,查看提交的url和參數(shù),通常使用的是Chrome的開發(fā)者工具中的Network, Fiddler等。
- 登錄后跳轉(zhuǎn)的url。
參數(shù)探索
首先看看這個登錄頁面,也就是我們登錄時的url地址。
看到這個頁面,我們也可以大概猜測下請求服務(wù)器時傳遞了幾個字段,很明顯有:用戶名、密碼、驗證碼以及“記住我”這幾個值。那么實際上有哪些呢?下面來分分析下。
首先查看一下HTML源碼,Google里可以使用CTRL+U查看,然后使用CTRL+F輸入input看看有哪些字段值,詳情如下:
通過源碼,我們可以看到,在請求服務(wù)器的過程中還攜帶了一個隱藏字段”_xsrf”。那么現(xiàn)在的問題是:這些參數(shù)在傳遞時是以什么名字傳遞的呢?這就需要借用其他工具抓包進行分析了。筆者是Windows系統(tǒng),這里使用的是Fiddler(當(dāng)然,你也可以使用其他的)。
抓包過程比較繁瑣,因為抓到的東西比較多,很難快速的找到需要的信息。關(guān)于fiddler,很容易使用,有過不會,可以去百度搜一下。為了防止其他信息干擾,我們先將fiddler中的記錄清除,然后輸入用戶名(筆者使用的是郵箱登錄)、密碼等信息登錄,相應(yīng)的在fiddler中會有如下結(jié)果:
備注:如果是使用手機登錄,則對應(yīng)fiddler中的url是“/login/phone_num”。
為了查看詳細(xì)的請求參數(shù),我們左鍵單機“/login/email”,可以看到下列信息:
請求方式為POST,請求的url為https://www.zhihu.com/login/email
。而從From Data可以看出,相應(yīng)的字段名稱如下:
- _xsrf
- captcha
- password
- remember
對于這五個字段,代碼中email、password以及captcha都是手動輸入的,remember初始化為true。剩下的_xsrf則可以根據(jù)登錄頁面的源文件,取input為_xsrf的value值即可。
對于驗證碼,則需要通過額外的請求,該鏈接可以通過定點查看源碼看出:
鏈接為https://www.zhihu.com/captcha.gif?type=login
,這里省略了ts(經(jīng)測試,可省略掉)。現(xiàn)在,可以使用代碼進行模擬登錄。
溫馨提示:如果使用的是手機號碼進行登錄,則請求的url為https://www.zhihu.com/login/phone_num
,同時email字段名稱將變成“phone_num”。
模擬源碼
在編寫代碼實現(xiàn)知乎登錄的過程中,筆者將一些功能封裝成了一個簡單的類WSpider,以便復(fù)用,文件名稱為WSpider.py。
# -*- coding: utf-8 -*- """ Created on Thu Nov 02 14:01:17 2016 @author: liudiwei """ import urllib import urllib2 import cookielib import logging class WSpider(object): def __init__(self): #init params self.url_path = None self.post_data = None self.header = None self.domain = None self.operate = None #init cookie self.cookiejar = cookielib.LWPCookieJar() self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookiejar)) urllib2.install_opener(self.opener) def setRequestData(self, url_path=None, post_data=None, header=None): self.url_path = url_path self.post_data = post_data self.header = header def getHtmlText(self, is_cookie=False): if self.post_data == None and self.header == None: request = urllib2.Request(self.url_path) else: request = urllib2.Request(self.url_path, urllib.urlencode(self.post_data), self.header) response = urllib2.urlopen(request) if is_cookie: self.operate = self.opener.open(request) resText = response.read() return resText """ Save captcha to local """ def saveCaptcha(self, captcha_url, outpath, save_mode='wb'): picture = self.opener.open(captcha_url).read() #用openr訪問驗證碼地址,獲取cookie local = open(outpath, save_mode) local.write(picture) local.close() def getHtml(self, url): page = urllib.urlopen(url) html = page.read() return html """ 功能:將文本內(nèi)容輸出至本地 @params content:文本內(nèi)容 out_path: 輸出路徑 """ def output(self, content, out_path, save_mode="w"): fw = open(out_path, save_mode) fw.write(content) fw.close() """#EXAMPLE logger = createLogger('mylogger', 'temp/logger.log') logger.debug('logger debug message') logger.info('logger info message') logger.warning('logger warning message') logger.error('logger error message') logger.critical('logger critical message') """ def createLogger(self, logger_name, log_file): # 創(chuàng)建一個logger logger = logging.getLogger(logger_name) logger.setLevel(logging.INFO) # 創(chuàng)建一個handler,用于寫入日志文件 fh = logging.FileHandler(log_file) # 再創(chuàng)建一個handler,用于輸出到控制臺 ch = logging.StreamHandler() # 定義handler的輸出格式formatter formatter = logging.Formatter('%(asctime)s | %(name)s | %(levelname)s | %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) # 給logger添加handler logger.addHandler(fh) logger.addHandler(ch) return logger
關(guān)于模擬登錄知乎的源碼,保存在zhiHuLogin.py文件,內(nèi)容如下:
# -*- coding: utf-8 -*- """ Created on Thu Nov 02 17:07:17 2016 @author: liudiwei """ import urllib from WSpider import WSpider from bs4 import BeautifulSoup as BS import getpass import json import WLogger as WLog """ 2016.11.03 由于驗證碼問題暫時無法正常登陸 2016.11.04 成功登錄,期間出現(xiàn)下列問題 驗證碼錯誤返回:{ "r": 1, "errcode": 1991829, "data": {"captcha":"驗證碼錯誤"}, "msg": "驗證碼錯誤" } 驗證碼過期:{ "r": 1, "errcode": 1991829, "data": {"captcha":"驗證碼回話無效 :(","name":"ERR_VERIFY_CAPTCHA_SESSION_INVALID"}, "msg": "驗證碼回話無效 :(" } 登錄:{"r":0, "msg": "登錄成功"} """ def zhiHuLogin(): spy = WSpider() logger = spy.createLogger('mylogger', 'temp/logger.log') homepage = r"https://www.zhihu.com/" html = spy.opener.open(homepage).read() soup = BS(html, "html.parser") _xsrf = soup.find("input", {'type':'hidden'}).get("value") #根據(jù)email和手機登陸得到的參數(shù)名不一樣,email登陸傳遞的參數(shù)是‘email',手機登陸傳遞的是‘phone_num' username = raw_input("Please input username: ") password = getpass.getpass("Please input your password: ") account_name = None if "@" in username: account_name = 'email' else: account_name = 'phone_num' #保存驗證碼 logger.info("save captcha to local machine.") captchaURL = r"https://www.zhihu.com/captcha.gif?type=login" #驗證碼url spy.saveCaptcha(captcha_url=captchaURL, outpath="temp/captcha.jpg") #temp目錄需手動創(chuàng)建 #請求的參數(shù)列表 post_data = { '_xsrf': _xsrf, account_name: username, 'password': password, 'remember_me': 'true', 'captcha':raw_input("Please input captcha: ") } #請求的頭內(nèi)容 header ={ 'Accept':'*/*' , 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8', 'X-Requested-With':'XMLHttpRequest', 'Referer':'https://www.zhihu.com/', 'Accept-Language':'en-GB,en;q=0.8,zh-CN;q=0.6,zh;q=0.4', 'Accept-Encoding':'gzip, deflate, br', 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36', 'Host':'www.zhihu.com' } url = r"https://www.zhihu.com/login/" + account_name spy.setRequestData(url, post_data, header) resText = spy.getHtmlText() jsonText = json.loads(resText) if jsonText["r"] == 0: logger.info("Login success!") else: logger.error("Login Failed!") logger.error("Error info ---> " + jsonText["msg"]) text = spy.opener.open(homepage).read() #重新打開主頁,查看源碼可知此時已經(jīng)處于登錄狀態(tài) spy.output(text, "out/home.html") #out目錄需手動創(chuàng)建 if __name__ == '__main__': zhiHuLogin()
關(guān)于源碼的分析,可以參考代碼中的注解。
運行結(jié)果
在控制臺中運行python zhiHuLogin.py,然后按提示輸入相應(yīng)的內(nèi)容,最后可得到以下不同的結(jié)果(舉了三個實例):
結(jié)果一:密碼錯誤
結(jié)果二:驗證碼錯誤
結(jié)果三:成功登錄
通過代碼,可以成功的登錄到知乎,接著如果要爬取知乎里面的內(nèi)容,就比較方便了。
以上就是利用Python實現(xiàn)模擬登錄知乎的詳細(xì)內(nèi)容,更多關(guān)于Python模擬登錄知乎的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于keras中keras.layers.merge的用法說明
這篇文章主要介紹了關(guān)于keras中keras.layers.merge的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python調(diào)用C語言開發(fā)的共享庫方法實例
這篇文章主要介紹了Python調(diào)用C語言開發(fā)的共享庫方法實例,本文同時給出了C語言和Python調(diào)用簡單實例,需要的朋友可以參考下2015-03-03五個Pandas?實戰(zhàn)案例帶你分析操作數(shù)據(jù)
pandas是基于NumPy的一種工具,該工具是為了解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的。Pandas納入了大量庫和一些標(biāo)準(zhǔn)的數(shù)據(jù)模型,提供了高效操作大型數(shù)據(jù)集的工具。pandas提供大量快速便捷地處理數(shù)據(jù)的函數(shù)和方法。你很快就會發(fā)現(xiàn),它是使Python強大而高效的數(shù)據(jù)分析環(huán)境的重要因素之一2022-01-01Windows 7下Python Web環(huán)境搭建圖文教程
這篇文章主要為大家詳細(xì)介紹了Windows 7下Python Web環(huán)境搭建圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03python subprocess 殺掉全部派生的子進程方法
下面小編就為大家?guī)硪黄猵ython subprocess 殺掉全部派生的子進程方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01python http服務(wù)flask架構(gòu)實用代碼詳解分析
本篇文章主要分享一個python的簡單http服務(wù)flask架構(gòu)。目前主流的python的服務(wù)框架有django、flask,相較于django來說,flask更小巧玲瓏。至于并發(fā)的問題,使用了gevent協(xié)程io進行處理2021-10-10