利用python爬取豆瓣電影top250的相關(guān)信息,包括電影詳情鏈接,圖片鏈接,影片中文名,影片外國(guó)名,評(píng)分,評(píng)價(jià)數(shù),概況,導(dǎo)演,主演,年份,地區(qū),類別這12項(xiàng)內(nèi)容,然后將爬取的信息寫入excel表中。基本上爬取結(jié)果還是挺好的。具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#!/usr/bin/python #-*- coding: utf-8 -*- import sys reload (sys) sys.setdefaultencoding( 'utf8' ) from bs4 import beautifulsoup import re import urllib2 import xlwt #得到頁(yè)面全部?jī)?nèi)容 def askurl(url): request = urllib2.request(url) #發(fā)送請(qǐng)求 try : response = urllib2.urlopen(request) #取得響應(yīng) html = response.read() #獲取網(wǎng)頁(yè)內(nèi)容 #print html except urllib2.urlerror, e: if hasattr (e, "code" ): print e.code if hasattr (e, "reason" ): print e.reason return html #獲取相關(guān)內(nèi)容 def getdata(baseurl): findlink = re. compile (r '<a href="(.*?)" rel="external nofollow" >' ) #找到影片詳情鏈接 findimgsrc = re. compile (r '<img.*src="(.*jpg)"' ,re.s) #找到影片圖片 findtitle = re. compile (r '<span class="title">(.*)</span>' ) #找到片名 #找到評(píng)分 findrating = re. compile (r '<span class="rating_num" property="v:average">(.*)</span>' ) #找到評(píng)價(jià)人數(shù) findjudge = re. compile (r '<span>(\d*)人評(píng)價(jià)</span>' ) #找到概況 findinq = re. compile (r '<span class="inq">(.*)</span>' ) #找到影片相關(guān)內(nèi)容:導(dǎo)演,主演,年份,地區(qū),類別 findbd = re. compile (r '<p class="">(.*?)</p>' ,re.s) #去掉無關(guān)內(nèi)容 remove = re. compile (r ' |\n|</br>|\.*' ) datalist = [] for i in range ( 0 , 10 ): url = baseurl + str (i * 25 ) html = askurl(url) soup = beautifulsoup(html, "html.parser" ) for item in soup.find_all( 'div' , class_ = 'item' ): #找到每一個(gè)影片項(xiàng) data = [] item = str (item) #轉(zhuǎn)換成字符串 #print item link = re.findall(findlink,item)[ 0 ] data.append(link) #添加詳情鏈接 imgsrc = re.findall(findimgsrc,item)[ 0 ] data.append(imgsrc) #添加圖片鏈接 titles = re.findall(findtitle,item) #片名可能只有一個(gè)中文名,沒有外國(guó)名 if ( len (titles) = = 2 ): ctitle = titles[ 0 ] data.append(ctitle) #添加中文片名 otitle = titles[ 1 ].replace( " / " ,"") #去掉無關(guān)符號(hào) data.append(otitle) #添加外國(guó)片名 else : data.append(titles[ 0 ]) #添加中文片名 data.append( ' ' ) #留空 rating = re.findall(findrating,item)[ 0 ] data.append(rating) #添加評(píng)分 judgenum = re.findall(findjudge,item)[ 0 ] data.append(judgenum) #添加評(píng)論人數(shù) inq = re.findall(findinq,item) #可能沒有概況 if len (inq)! = 0 : inq = inq[ 0 ].replace( "。" ,"") #去掉句號(hào) data.append(inq) #添加概況 else : data.append( ' ' ) #留空 bd = re.findall(findbd,item)[ 0 ] bd = re.sub(remove,"",bd) bd = re.sub( '<br>' , " " ,bd) #去掉<br> bd = re.sub( '/' , " " ,bd) #替換/ #data.append(bd) words = bd.split( " " ) for s in words: if len (s)! = 0 and s! = ' ' : #去掉空白內(nèi)容 data.append(s) #主演有可能因?yàn)閷?dǎo)演內(nèi)容太長(zhǎng)而沒有 if ( len (data)! = 12 ): data.insert( 8 , ' ' ) #留空 datalist.append(data) return datalist #將相關(guān)數(shù)據(jù)寫入excel中 def savedata(datalist,savepath): book = xlwt.workbook(encoding = 'utf-8' ,style_compression = 0 ) sheet = book.add_sheet( '豆瓣電影top250' ,cell_overwrite_ok = true) col = ( '電影詳情鏈接' , '圖片鏈接' , '影片中文名' , '影片外國(guó)名' , '評(píng)分' , '評(píng)價(jià)數(shù)' , '概況' , '導(dǎo)演' , '主演' , '年份' , '地區(qū)' , '類別' ) for i in range ( 0 , 12 ): sheet.write( 0 ,i,col[i]) #列名 for i in range ( 0 , 250 ): data = datalist[i] for j in range ( 0 , 12 ): sheet.write(i + 1 ,j,data[j]) #數(shù)據(jù) book.save(savepath) #保存 def main(): baseurl = 'https://movie.douban.com/top250?start=' datalist = getdata(baseurl) savapath = u '豆瓣電影top250.xlsx' savedata(datalist,savapath) main() |
excel表部分內(nèi)容如下:
以上所述是小編給大家介紹的python爬取豆瓣電影top250實(shí)例詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://blog.csdn.net/Fighting_No1/article/details/50926008