前兩天用python2寫的一個小爬蟲
主要實現了從http://www.cbooo.cn/Alltimedomestic這么個網頁中爬取每一部電影的票房信息等,以及在豆瓣上該電影的評分信息
代碼如下
# -*- coding:utf-8 -*- from __future__ import print_function import urllib2 import re ''' TODO:error 10060 ''' def fixEnglishName(name): ooo=re.compile("&amp;#246;") space=re.compile("·") if(len(space.findall(name))!=0): nameTmp=re.sub("·"," ",name) if(len(ooo.findall(nameTmp))!=0): nameTtmp=re.sub("&amp;#246;","o",nameTmp) return nameTtmp return nameTmp if(len(ooo.findall(name))!=0): nameTttmp=re.sub("&amp;#246;","o",name) return nameTttmp return name print(u'影片名;影片類型;國家及地區;總票房;平均票價;上映日期;場均人次;導演;主演;制作公司;發行公司;豆瓣評分 評論數;5星百分比;4星百分比;3星百分比;2星百分比;1星百分比') pre_url="http://www.cbooo.cn/BoxOffice/getInland?pIndex=" for index in range(5): aft_url=str(index+1)+"&t=0" url=pre_url+aft_url response = urllib2.urlopen(url) pageCode=response.read().decode('utf-8') pattern=re.compile(".*?ID\":\"(.*?)\",\".*?\":\"(.*?)\",\".*?\":\"(.*?)\",\".*?\":\"(.*?)\",\".*?\":\"(.*?)\",\".*?rice\":\"(.*?)\",\".*?\":\"(.*?)\",\".*?\":\"(.*?)\"",re.S) items=re.findall(pattern, pageCode) #pageFilms = [] '''item[0]:id,item[1]:名字,item[2]:類型,item[3]:國家及地區,item[4]:總票房,item[5]:平均票價,item[6]:上映日期,item[7]:場均人次''' for item in items: print(item[1]+";"+item[2]+";"+item[3]+";"+item[4]+";"+item[5]+";"+item[6]+";"+item[7],end=";") #pageFilms.append([item[0].strip(),item[1].strip(),item[2].strip(),item[3].strip(),item[4].strip(),item[5].strip(),item[6].strip(),item[7].strip()]) filmUrl='http://www.cbooo.cn/m/'+str(item[0]) '''filmUrl:藝恩網電影頁面''' filmResponse=urllib2.urlopen(filmUrl) filmPageCode=filmResponse.read().decode('utf-8') #filmPattern=re.compile("onerror=\".*?\"borbg.pad02\".*?title=\"(.*?)\">.*?title=\"(.*?)\">.*?<dt>.*?title=\"(.*?)\">.*?<dt>.*?title=\"(.*?)\">",re.S) filmPattern = re.compile( "onerror=\".*?\"borbg.pad02\".*?title=\"(.*?)\">.*?<dd>.*?title=\"(.*?)\">.*?<dt>.*?title=\"(.*?)\">.*?<dt>.*?title=\"(.*?)\">", re.S) filmItems=re.findall(filmPattern,filmPageCode) replaceSpace = re.compile("·") for filmItem in filmItems: print(fixEnglishName(filmItem[0]),end=';') print(fixEnglishName(filmItem[1]),end=';') print(filmItem[2],end=';') print(filmItem[3],end=';') dbTotal_Url="https://movie.douban.com/j/subject_suggest?q="+item[1]#電影名搜索鏈接 dbResponse = urllib2.urlopen(dbTotal_Url.encode("utf-8")) dbCode = dbResponse.read().decode('utf-8') dbTmp = re.sub(re.compile("\\\/"), "/", dbCode) dbPattern = re.compile("url\":\"(.*?)\",\"", re.S) dbItems = re.findall(dbPattern, dbTmp) for dbItem in dbItems: '''訪問該頁面並提取評分,評論數''' dbFilmResponse=urllib2.urlopen(dbItem.strip()) dbFilmPageCode=dbFilmResponse.read().decode('utf-8') dbFilmPattern=re.compile("property=\"v:average\">(.*?)<.*?votes\">(.*?)<.*?rating_per\">(.*?)%.*?rating_per\">(.*?)%.*?rating_per\">(.*?)%.*?rating_per\">(.*?)%.*?rating_per\">(.*?)%",re.S) dbFilmItems=re.findall(dbFilmPattern,dbFilmPageCode) '''dbFilmItem[0]:評分,dbFilmItem[1]:評論數,dbFilmItem[2]:5星百分比,dbFilmItem[3]:4星百分比,dbFilmItem[4]:3星百分比,dbFilmItem[5]:2星百分比,dbFilmItem[6]:1星百分比''' for dbFilmItem in dbFilmItems: for x in range(7): print(dbFilmItem[x],end=';') break print ('') '''換行'''
爬取過程還算順利,期間遇到了一些小麻煩:
一部分導演的名字帶有空格,由於編碼的問題輸出結果會變成·
《一條狗的使命》的導演萊塞·霍爾斯道姆先生的英文名中某個奇怪字符(貌似是瑞典字符?)會輸出成為&amp;#246;
以上都通過fixEnglishName函數進行了轉化.
由於輸出后的結果想要直接拿到excel里使用,為了進行輸出格式的控制,通過from __future__ import print_function
將print xxx 替換為 print(xxx,end='xx'),其中第二個參數省略則默認是換行
豆瓣信息的獲取是從藝恩網捕獲到電影名后放入豆瓣電影搜索,再進入詳情頁獲得
關於最上面的'''todo''',找我做這個小爬蟲的同學在運行我程序的時候經常會出現error10060,本來想通過多次請求連接解決來着,但是寫完后我這里已經有了完整的數據了,就不需要再對本程序進行完善了