要離線下載易百教程網站中的所有關於Python的教程,需要將Python教程的首頁作為種子url:http://www.yiibai.com/python/,然后按照廣度優先(廣度優先,使用隊列;深度優先,使用棧),依次爬取每一篇關於Python的文章。為了防止同一個鏈接重復爬取,使用集合來限制同一個鏈接只處理一次。
使用正則表達式提取網頁源碼里邊的文章標題和文章url,獲取到了文章的url,使用Python根據url生成html文件十分容易。
-
import re
-
import urllib.request
-
import urllib
-
from collections
import deque
-
# 保存文件的后綴
-
SUFFIX=
'.html'
-
# 提取文章標題的正則表達式
-
REX_TITLE=
r'<title>(.*?)</title>'
-
# 提取所需鏈接的正則表達式
-
REX_URL=
r'/python/(.+?).html'
-
# 種子url,從這個url開始爬取
-
BASE_URL=
'http://www.yiibai.com/python/'
-
-
-
# 將獲取到的文本保存為html文件
-
def saveHtml(file_name,file_content):
-
# 注意windows文件命名的禁用符,比如 /
-
with open (file_name.replace(
'/',
'_')+SUFFIX,
"wb")
as f:
-
# 寫文件用bytes而不是str,所以要轉碼
-
f.write(bytes(file_content, encoding =
"utf8"))
-
# 獲取文章標題
-
def getTitle(file_content):
-
linkre = re.search(REX_TITLE,file_content)
-
if(linkre):
-
print(
'獲取文章標題:'+linkre.group(
1))
-
return linkre.group(
1)
-
-
# 爬蟲用到的兩個數據結構,隊列和集合
-
queue = deque()
-
visited = set()
-
# 初始化種子鏈接
-
queue.append(BASE_URL)
-
count =
0
-
-
while queue:
-
url = queue.popleft()
# 隊首元素出隊
-
visited |= {url}
# 標記為已訪問
-
-
print(
'已經抓取: ' + str(count) +
' 正在抓取 <--- ' + url)
-
count +=
1
-
urlop = urllib.request.urlopen(url)
-
# 只處理html鏈接
-
if
'html'
not
in urlop.getheader(
'Content-Type'):
-
continue
-
-
# 避免程序異常中止
-
try:
-
data = urlop.read().decode(
'utf-8')
-
title=getTitle(data);
-
# 保存文件
-
saveHtml(title,data)
-
except:
-
continue
-
-
# 正則表達式提取頁面中所有鏈接, 並判斷是否已經訪問過, 然后加入待爬隊列
-
linkre = re.compile(REX_URL)
-
for sub_link
in linkre.findall(data):
-
sub_url=BASE_URL+sub_link+SUFFIX;
-
# 已經訪問過,不再處理
-
if sub_url
in visited:
-
pass
-
else:
-
# 設置已訪問
-
visited |= {sub_url}
-
# 加入隊列
-
queue.append(sub_url)
-
print(
'加入隊列 ---> ' + sub_url)
