Python
Python簡單爬蟲實現
一.什麼是爬蟲
一段自動抓取互聯網信息的程序
二.爬蟲的價值
互聯網數據,爲我使用
三.簡單爬蟲架構
四.Python簡單爬蟲架構動態運行流程
五.URL管理器
1.管理待抓取URL集合和已抓取URL集合
-- 防止重複抓取,防止循環抓取
#### 2.實現URL管理器(三種方式 內存,關係數據庫,緩存數據庫)
六.Python爬蟲網頁下載器簡介
將互聯網上的URL對應的網頁下載到本地的工具
python有哪幾種網頁下載器(urllib2 requests)
七. python爬蟲urllib2下載網頁的三種方法
1.最簡潔的方法
import urllib2
#直接請求
response = urllib2.urlopen('http://www.baidu.com')
#獲取狀態嗎,如果是200 表示獲取成功
print response.getcode()
#讀取內容
cont = response.read()
2. 添加data 、http header
import urllib2
# 創建Request對象
request = urllib2.Request(url)
# 添加數據
request.add_data('a','1')
#添加http的header
request.add_header('User-Agent','Mozilla/5.0')
#發送請求獲取數據
response = urllib2.urlopen(request)
3.添加特殊情景的處理器
import urllib2,cookielb
#創建cookie容器
cj = cookielib.CookieJar()
#創建1個opener
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#創建urllib2安裝opener
urllib2.install_opener(opener)
#使用帶有cookie的urllib2訪問網頁
response = urllib2.urlopen('http://www.baidu.com')
八.python爬蟲urllib2實例代碼
import urllib2
import cookielib
url ="http://www.baidu.com"
print '第一種方法'
response1 = urllib2.urlopen(url)
print response1.getcode()
print len(response1.read())
print '第二種方法'
# 創建Request對象
request = urllib2.Request(url)
#添加http的header
request.add_header('User-Agent','Mozilla/5.0')
#發送請求獲取數據
response2 = urllib2.urlopen(request)
print response2.getcode();
print len(response2.read())
print '第三中方法'
cj = cookielib.CookieJar()
opener= urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(url)
print response3.getcode()
print len(response3.read())
執行結果
九.網頁解析器(正則表達式,html.parser,BeautifulSoup,lxml)
正則表達式 屬於模糊解析
html.parser ,BeautifulSoup,lxml屬於結構化解析
十.BeautifulSoup簡介安裝
1.Beautiful Soup是python第三方庫,用於從html或xml中提取數據,官網網址:http://www.crummy.com/software/BeautifulSoup/
2.安裝
a.查看是否安裝
import bs4
print bs4
結果報錯 ImportError: No module named bs
b.打開終端
先裝pip (終端)輸入命令:sudo easy_install pip
安bs4 輸入命令:pip install beautifulsoup4
出現錯誤
OSError: [Errno 13] Permission denied:
輸入命令:sudo easy_install beautifulsoup4
這時把bs裝到了Mac系統自帶了Python2.7
把beautifulsoup4裝到Python3
輸入命令:pip3 install beautifulsoup4
3.語法
代碼
1.創建beautifulSoup對象
from bs4 import BeautifulSoup
# 根據HTML網頁字符串創建BeautifulSoup對象
soup = BeautifulSoup(
html_doc, #HTML文檔字符串
'html.parser' #HTML解析器
from_encoding='utf-8'#HTML文檔的編碼
)
2.搜索節點
# 方法: find_all(name,attrs,string)
#查找所有標籤爲a的節點
soup.find_all('a')
#查找所有標籤爲a,連接符合/view/123.html形式的節點
soup.find_all('a',href='/view/123.html')
#查找所有標籤爲div class爲abl 文字爲python節點
soup.find_all('div',class_ ='abc',string='python')
3.訪問信息
# 得到節點:<a href='1.html'>Python</a>
# 獲取查找到的節點的標籤名稱
node.name
# 獲取查找到的a接單的href屬性
node['href']
#查找到a節點的連接文字
node.get_text()
4.實例
# coding:utf-8
from bs4 import 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="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')
print '獲取所有鏈接'
links = soup.find_all('a')
for link in links:
print link.name,link['href'],link.get_text()
print '獲取lacie的鏈接'
link_node = soup.find('a',href='http://example.com/lacie')
print link_node.name,link_node['href'],link_node.get_text()
print '正則匹配'
link_node2 = soup.find('a',href=re.compile(r"ill"))
print link_node2.name,link_node2['href'],link_node2.get_text()
print '獲取p段落文字'
link_node3 = soup.find('p',class_='title')
print link_node3.name,link_node3.get_text()
運行結果
資源下載https://github.com/marven88cn/Python_Spider
注:以上源碼在https://github.com/marven88cn/Python_Spider下test1.py,test2.py,baike目錄下是一個玩轉的爬取百度百科詞條的demo