Python爬蟲基礎
前言
Python非常適合用來開發網頁爬蟲,理由如下:
1、抓取網頁本身的接口
相比與其他靜態編程語言,如java,c#,c++,python抓取網頁文檔的接口更簡潔;相比其他動態腳本語言,如perl,shell,python的urllib2包提供了較為完整的訪問網頁文檔的API。(當然ruby也是很好的選擇)
此外,抓取網頁有時候需要模擬瀏覽器的行為,很多網站對于生硬的爬蟲抓取都是封殺的。這是我們需要模擬user agent的行為構造合適的請求,譬如模擬用戶登陸、模擬session/cookie的存儲和設置。在python里都有非常優秀的第三方包幫你搞定,如Requests,mechanize
2、網頁抓取后的處理
抓取的網頁通常需要處理,比如過濾html標簽,提取文本等。python的beautifulsoap提供了簡潔的文檔處理功能,能用極短的代碼完成大部分文檔的處理。
其實以上功能很多語言和工具都能做,但是用python能夠干得最快,最干凈。
Life is short, you need python.
PS:python2.x和python3.x有很大不同,本文只討論python3.x的爬蟲實現方法。
爬蟲架構
架構組成
URL管理器:管理待爬取的url集合和已爬取的url集合,傳送待爬取的url給網頁下載器。
網頁下載器(urllib):爬取url對應的網頁,存儲成字符串,傳送給網頁解析器。
網頁解析器(BeautifulSoup):解析出有價值的數據,存儲下來,同時補充url到URL管理器。
運行流程
URL管理器
基本功能
- 添加新的url到待爬取url集合中。
- 判斷待添加的url是否在容器中(包括待爬取url集合和已爬取url集合)。
- 獲取待爬取的url。
- 判斷是否有待爬取的url。
- 將爬取完成的url從待爬取url集合移動到已爬取url集合。
存儲方式
1、內存(python內存)
待爬取url集合:set()
已爬取url集合:set()
2、關系數據庫(mysql)
urls(url, is_crawled)
3、緩存(redis)
待爬取url集合:set
已爬取url集合:set
大型互聯網公司,由于緩存數據庫的高性能,一般把url存儲在緩存數據庫中。小型公司,一般把url存儲在內存中,如果想要永久存儲,則存儲到關系數據庫中。
網頁下載器(urllib)
將url對應的網頁下載到本地,存儲成一個文件或字符串。
基本方法
新建baidu.py,內容如下:
import urllib.request
response = urllib.request.urlopen('命令行中執行 python baidu.py ,則可以打印出獲取到的頁面。
構造Request
上面的代碼,可以修改為:
import urllib.request
request = urllib.request.Request('攜帶參數
新建baidu2.py,內容如下:
import urllib.request
import urllib.parse
創建cookie容器
cj = http.cookiejar.CookieJar()
創建opener
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
給urllib.request安裝opener
urllib.request.install_opener(opener)
請求
根據html網頁字符串創建BeautifulSoup對象
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 href="
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc)
print(soup.prettify())</code></pre>
2、訪問節點
print(soup.title)
print(soup.title.name)
print(soup.title.string)
print(soup.title.parent.name)
print(soup.p)
print(soup.p['class'])</code></pre>
3、指定tag、class或id
print(soup.find_all('a'))
print(soup.find('a'))
print(soup.find(class_='title'))
print(soup.find(id="link3"))
print(soup.find('p',class_='title'))
4、從文檔中找到所有 <a> 標簽的鏈接
for link in soup.find_all('a'):
print(link.get('href'))
出現了警告,根據提示,我們在創建BeautifulSoup對象時,指定解析器即可。
soup = BeautifulSoup(html_doc,'html.parser')
5、從文檔中獲取所有文字內容
print(soup.get_text())
6、正則匹配
link_node = soup.find('a',href=re.compile(r"til"))
print(link_node)
后記
python爬蟲基礎知識,至此足夠,接下來,在實戰中學習更高級的知識。
書簽
Python開發簡單爬蟲
http://www.imooc.com/learn/563
The Python Standard Library
https://docs.python.org/3/library/index.html
Beautiful Soup 4.2.0 文檔
https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
為什么python適合寫爬蟲?
http://www.cnblogs.com/benzone/p/5854084.html
如何學習Python爬蟲[入門篇]?
https://zhuanlan.zhihu.com/p/21479334?refer=passer
你需要這些:Python3.x爬蟲學習資料整理
https://zhuanlan.zhihu.com/p/24358829?refer=passer
如何入門 Python 爬蟲?
https://www.zhihu.com/question/20899988
Python3.X 抓取網絡資源
http://www.baiduhome.net/lib/view/open1396062681294.html
python網絡請求和"HTTP Error 504:Fiddler - Receive Failure"
http://blog.csdn.net/guoguo527/article/details/50709244
怎么使用Fiddler抓取自己寫的爬蟲的包?
https://www.zhihu.com/question/52614615
fiddler對python腳本抓取https包時發生了錯誤?
https://www.zhihu.com/question/42104344?sort=created
HTTPS和HTTP的區別
http://blog.csdn.net/whatday/article/details/38147103
來自:http://www.cnblogs.com/voidking/p/python-crawler-base.html