我對python也是自學(xué)不久胞锰,平常都是用C,有正在學(xué)習(xí)C語言的朋友兢榨,可以進Q群121811911下載軟件資料和視頻嗅榕,我們一起進步。
所需工具準(zhǔn)備
*安裝fiddler
*安裝beautifulsoup4
pip install beautifulsoup4 -i https://pypi.douban.com/simple
*python 中的requests(標(biāo)準(zhǔn)庫)
beautifulsoup用于解析html文檔
bs4可以用于方便地解析html, xml等結(jié)構(gòu)化文檔吵聪,對于http的爬蟲凌那,我們最常用的功能,是解析html文檔吟逝。
如帽蝶,對于以下素材:
<pre style="margin: 8px 0px; color: rgb(51, 51, 51); background-color: rgb(238, 255, 204);">html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" id="link1">Elsie</a>,
<a class="sister" id="link2">Lacie</a> and
<a class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""</pre>
經(jīng)過構(gòu)造:
soup = bs4.BeautifulSoup(html_doc, "html.parser")
之后,標(biāo)簽被轉(zhuǎn)化為soup對象中的各個成員块攒。
其次励稳,有多種方法去定位或遍歷標(biāo)簽及標(biāo)簽的屬性佃乘。
#尋找所有a標(biāo)簽,并以list形式返回
In [10]: soup.find_all('a')
Out[10]:
[<a class="sister" id="link1">Elsie</a>,
<a class="sister" id="link2">Lacie</a>,
<a class="sister" id="link3">Tillie</a>]
對于tag對象驹尼,有類似字典的方法拿其對應(yīng)的屬性:
In [11]: for tag in soup.find_all('a'):
...: print(tag['href'])
...:
http://example.com/elsie
http://example.com/lacie
http://example.com/tillie
對于每個標(biāo)簽趣避,可以通過.text屬性,拿到其文本:
In [12]: list_a_tags = soup.find_all('a')
In [13]: list_a_tags[0].text
以下例子新翎,打印出所有class為story的p標(biāo)簽中的text內(nèi)容:
In [21]: for tag in list_p_tags:
...: if tag['class'][0] == 'story':
...: print(tag.text)
...:
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...
python中的requests模塊介紹
requests是python自帶的標(biāo)準(zhǔn)庫程帕,使用它,可以很方便得獲取和發(fā)送http包地啰。
import bs4
import requests
import sys
def main():
if len(sys.argv) != 2:
print('''
usage:
word_lookup.py <word>
''')
else:
url = 'http://dict.cn/' + sys.argv[1]
rsp = requests.get(url)
soup = bs4.BeautifulSoup(rsp.text, 'html.parser')
div_id_content = soup.find(id='content')
list_strong = div_id_content.find_all('strong')
for tag in list_strong:
print(tag.text)
if __name__ == "__main__":
main()
http中的get方式與post方式
get方式
get方式的請求愁拭,其客戶端(瀏覽器)發(fā)送的數(shù)據(jù),直接放在url的尾部亏吝,用戶可見岭埠。
post方式
post方式的請求,其客戶端數(shù)據(jù)顺呕,放在http包內(nèi)部枫攀,普通用戶不可見括饶。
requests的作者株茶,同時開發(fā)了一款用于測試的服務(wù)器,稱為httpbin图焰,會響應(yīng)各種http請求并回復(fù)启盛。可以使用www.httpbin.org技羔,也可以參考https://hub.docker.com/r/kennethreitz/httpbin/本地化安裝僵闯。
使用fiddler進行抓包及http協(xié)議分析
每一個session(一個http包),都包含了一個request藤滥,和一個response鳖粟,他們由都分為了兩部分:headers, data拙绊。
fiddler分析的協(xié)議過程向图,就是查看客戶端(瀏覽器)與服務(wù)端到底是如何通信發(fā)包的标沪。
爬蟲一般要盡量完美地模擬真實的瀏覽器發(fā)包過程榄攀。
cookie
cookie是為了網(wǎng)站(服務(wù)端)可以跨頁面記錄用戶信息發(fā)明的一種機制金句。
它使得服務(wù)端有權(quán)限,在客戶端創(chuàng)建記錄信息的小文件(cookies)违寞,而客戶端在與服務(wù)端通信的過程中偶房,會將這些cookies的內(nèi)容,一并讀取并發(fā)給對應(yīng)網(wǎng)站军浆。
這種機制,使得服務(wù)端可以跨頁面記錄用戶的信息瘾敢。
request庫中已經(jīng)提供了可以長期保持狀態(tài)的鏈接方式:
my_session = requests.session() #拿到session對象
my_session.post(url, header_dict, data_dict) #同普通的post或get方法
#不同之處在于, session的記錄有連續(xù)性(自動保存了cookie等)
當(dāng)要爬取有身份驗證(需要登陸)的網(wǎng)站的信息時,一般有兩個大方向:
其一簇抵,通過分析http協(xié)議庆杜,完整模擬出發(fā)包登陸的過程碟摆。
第二,先手工登陸典蜕,再通過復(fù)制cookie,用于之后的session中愉舔,模擬登陸狀態(tài)下的抓取钢猛。
我對python也是自學(xué)不久轩缤,平常都是用C,有正在學(xué)習(xí)C語言的朋友火的,可以進Q群121811911下載軟件資料和視頻壶愤,我們一起進步。