隨便說點什么
因為在學python,所有自然而然的就掉進了爬蟲這個坑里,好吧,主要是因為我覺得爬蟲比較酷,才入坑的。
想想看,你可以批量自動的采集互聯網上海量的資料數據,是多么令人激動啊!
所以我就被這塊大蛋糕吸引過來了 :)
?
想學爬蟲自然要去找學習資料了,不過網上有很多,我找了不少,個人覺得崔慶才的爬蟲教程寫得不錯。起碼對我來說,入門是夠了。
感興趣的朋友可以點進鏈接看看:Python爬蟲學習系列教程? ?<==這位兄臺博客做得也很好
掌握了基本的爬蟲知識,主要是urllib,urlib2,re 這些庫,以及Request(),urlopen()的基本用法之后,我就開始尋找爬取目標。
?
正好女朋友的哥哥讓她幫忙下載小說,找我推薦幾本,我一時也不知道他哥喜歡什么類型的,心想干脆找個小說網批量下載一些小說。
于是說干就干,用了一個中午時間寫了一個粗糙的爬蟲腳本,啟動發現可以運行,讓腳本在這兒跑著,我回去躺在床上呼呼大睡。
起來之后發現腳本遇到錯誤,停掉了,于是debug,干掉bug之后跑起來陸續又發現幾個錯誤,于是干脆在一些容易出錯的地方,例如urlopen()請求服務器的地方,本地write()寫入的地方(是的,這也會有超時錯誤!)加入了try-except捕獲錯誤進行處理,另外加入了socket.timeout網絡超時限制,修修補補之后總算可以順暢的運行。
如此,運行了兩天,爬蟲腳本把這個小說網上的幾乎所有小說都下載到了本地,一共27000+本小說,一共40G。
自此,大功告成,打包發了過去。另外說一句,百度云真坑,每次上傳數量有限制,分享給朋友文件數量有限制,分享的文件夾大小也有限制,害的我還得整成壓縮版才能分享。
下載界面
下面附上代碼
1 #coding=utf-8
2 import urllib
3 import urllib2
4 import re
5 import os
6
7 webroot = 'http://www.xuanshu.com'
8
9 for page in range(20,220):
10 print '正在下載第'+str(page)+'頁小說'
11
12 url = 'http://www.xuanshu.com/soft/sort02/index_'+str(page)+'.html'
13 headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' }
14 try:
15 request = urllib2.Request(url,headers=headers)
16 response = urllib2.urlopen(request,timeout=180)
17 #print response.read()
18 except urllib2.URLError, e:
19 if hasattr(e,"code"):
20 print e.code
21 if hasattr(e,"reason"):
22 print e.reason
23
24 html = response.read().decode('utf-8')
25 #print html
26 pattern = re.compile(u'<li>.*?<div class="s">.*?target="_blank">(.*?)</a><br />大小:(.*?)<br>.*?</em><br>更新:(.*?)</div>.*?<a href="(.*?)"><img.*?>(.*?)</a>.*?<div class="u">(.*?)</div>',re.S)
27 items = re.findall(pattern,html)
28 #print items
29
30 for item in items:
31 try:
32 book_auther = item[0].encode('gbk')
33 book_size = item[1].encode('gbk')
34 book_updatetime = item[2].encode('gbk')
35 book_link = item[3].encode('gbk')
36 book_name = item[4].encode('gbk')
37 book_note = item[5].encode('gbk')
38
39 book_full_link = webroot + book_link # 構建書的絕對地址
40
41 #請求地址
42 try:
43 request = urllib2.Request(book_full_link,headers=headers)
44 response = urllib2.urlopen(request,timeout=180)
45 except urllib2.URLError, e:
46 if hasattr(e,"code"):
47 print e.code
48 if hasattr(e,"reason"):
49 print e.reason
50 html = response.read().decode('utf-8')
51 #print html
52 pattern = re.compile('<a class="downButton.*?<a class="downButton" href=\'(.*?)\'.*?Txt.*?</a>',re.S)
53 down_link = re.findall(pattern,html)
54 print book_name
55 print down_link
56
57 # down txt
58 try:
59 request = urllib2.Request(down_link[0].encode('utf-8'),headers=headers)
60 response = urllib2.urlopen(request,timeout=180)
61 except urllib2.URLError, e:
62 if hasattr(e,"code"):
63 print e.code
64 if hasattr(e,"reason"):
65 print e.reason
66 try:
67 fp = open(book_name+'.txt','w')
68 except IOError,e:
69 pattern = re.compile('<strong>.*?>(.*?)<.*?</strong>',re.S)
70 book_name = re.findall(pattern,book_name)
71 fp = open(book_name[0]+'.txt','w')
72 print 'start download'
73 fp.write(response.read())
74 print 'down finish\n'
75 fp.close()
76 except Exception,e:
77 print '該條目解析出現錯誤,忽略'
78 print e
79 print ''
80 fp = open('error.log','a')
81 fp.write('page:'+str(page)+'\n')
82 fp.write(item[4].encode('gbk'))
83 #fp.write(e)
84 fp.write('\nThere is an error in parsing process.\n\n')
85 fp.close()
86
87
注意
該腳本只定向抓取“選書網”小說站,“玄幻奇幻”分類下的小說。供網友們參考,可自行修改。
寫得比較粗糙,勿噴……
Freecode# :?www.cnblogs.com/yym2013