def html(self):
try:
url = self.baseurl + str(self.page) + '&filter=&type='
user_agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36'
headers = { 'User-Agent' : user_agent }
request = urllib2.Request(url, headers = headers)
response = urllib2.urlopen(request)
content = response.read()
pattern = re.compile('<em.*?class="">(.*?)</em>.*?<span.*?class="title">(.*?)</span>.*?<p.*?class="">\s(.*?)<br>.*?\s(.*?)</p>.*?<em>(.*?)</em>.*?<span.*?class="inq">(.*?)</span>', re.S)
items = re.findall(pattern, content)
return items
except urllib2.URLError, e:
if hasattr(e, "reason"):
print u"豆瓣鏈接錯誤,錯誤原因", e.reason
return None
def wtext(self,items):
remove = re.compile(' | {4}|;') #在獲取的排名中去除   ; 空格
for item in items:
print (' '.join(map(lambda s: re.sub(remove,'',s),item)))
files = open(self.filename,'a')
files.write((' '.join(map(lambda s: re.sub(remove,'',s),item))))
files.write('\n')
files.write('\n')
files.close()
def strat(self):
self.wtext(self.html())
print u"豆瓣電影TOP爬取完畢"
print u"輸入任意鍵結束,'V'爬取書籍"
fir = raw_input('>')
if fir == 'V':
bookstyle()
else:
print "over"
---------------------------------------------------------------book--
def bookstyle(): #豆瓣爬取得書籍目錄
print u"請輸入你要爬的書籍類型,查看所有類型輸入 'V' "
booktypes = raw_input(u">")
while booktypes == 'V':
print "###########################################"
print u"小說 外國文學 文學 隨筆"
print u"中國文學 經典 散文 日本文學"
print u"村上春樹 童話 詩歌 雜文"
print u"王小波 張愛玲 兒童文學 古典文學"
print u"余華 名著 錢鐘書 當代文學"
print u"魯迅 外國名著 詩詞 茨威格"
print u"米蘭·昆德拉 杜拉斯 港臺"
print u"流行 · · · · · ·"
print u" "
print u"漫畫 繪本 推理 青春"
print u"言情 科幻 懸疑 東野圭吾"
print u"武俠 韓寒 奇幻 日本漫畫"
print u"耽美 亦舒 三毛 安妮寶貝"
print u"網絡小說 郭敬明 穿越 推理小說"
print u"金庸 輕小說 幾米 阿加莎·克里斯蒂"
print u"幾米 張小嫻 魔幻 青春文學"
print u"J.K.羅琳 高木直子 滄月 古龍"
print u"科幻小說 落落 張悅然 蔡康永"
print u"文化 · · · · · ·"
print u" "
print u"歷史 心理學 哲學 傳記"
print u"文化 社會學 設計 藝術"
print u"政治 社會 建筑 宗教"
print u"電影 數學 政治學 回憶錄"
print u"思想 國學 中國歷史 音樂"
print u"人文 戲劇 人物傳記 繪畫"
print u"藝術史 佛教 軍事 西方哲學"
print u"近代史 二戰 自由主義 考古"
print u"美術"
print u"生活 · · · · · ·"
print u" "
print u"愛情 旅行 生活 勵志"
print u"成長 攝影 心理 女性"
print u"職場 美食 游記 教育"
print u"靈修 情感 健康 手工"
print u"養生 兩性 家居 人際關系"
print u"自助游"
print u"經管 · · · · · ·"
print
print u"經濟學 管理 經濟 金融"
print u"商業 投資 營銷 理財"
print u"創業 廣告 股票 企業史"
print u"策劃"
print u"科技 · · · · · ·"
print u" "
print u"科普 互聯網 編程 科學"
print u"交互設計 用戶體驗 算法 web"
print u"科技 UE 通信 UCD"
print u"交互 神經網絡 程序"
print"###################################"
booktypes = raw_input(u">")
strs = unicode(booktypes, 'gbk') #windows輸入的是GBK編碼先轉換為unicode
strs = strs.encode('UTF-8') #再將unicode轉換為UTF-8
types = urllib.quote(strs)
print u"請輸入你要爬的書籍排名頁數(TOP15一頁)"
bookpage = input(u">")
if bookpage == 1:
page = 0
else:
page = (bookpage - 1) * 15
script = book(types,page)
script.strat()
-------------------------------------------movies----------------------------
def optmove(): #選擇要爬熱門還是top
print u"你想爬近期熱門電影還是總電影top?"
print u"熱門電影輸入 'H', 總TOP輸入 'T'"
opt = raw_input('>')
while opt != 'H' and opt != 'T':
print u"輸入有誤,熱門電影輸入 'H', 總TOP輸入 'T'"
opt = raw_input('>')
if opt == "H":
moviestyle()
else:
topmove()
def topmove():
print u"獲取豆瓣電影TOP250"
print u"每頁top25部影,共10頁要爬第幾頁?"
pages = input(">")
if pages == 1:
page = 0
elif pages > 1 and pages <= 10:
page = (pages - 1) 25
else:
page = (10 - 1) 25
topmove = TOPmove(page)
topmove.strat()
def moviestyle():
print u'獲取熱門新片排行'
movies = movie()
movies.strat()
--------------------------------------------start-----------------------------
print "##############################################"
print u"名字:豆瓣圖書電影爬蟲機器人"
print u"版本:2.5"
print u"作者:jetou"
print u"說明:按照指令操作即可"
print u"功能:輸入豆瓣圖書和電影的分類和頁數爬取豆瓣書單并保存txt文件"
print "##############################################"
print u"要爬豆瓣書籍或者是豆瓣電影呢?'book' or 'movie'?"
classify = raw_input('book or movie?')
while classify != 'book' and classify != 'movie' : #if not book or movie
print u"無效指令請輸入book or movie"
classify = raw_input('>')
if classify == 'book':
bookstyle()
else:
optmove()
except urllib2.URLError, e:
if hasattr(e,"code"):
print e.code
if hasattr(e,"reason"):
print e.reason
</pre>
QQ圖片20150604161654.png