Python爬蟲(chóng)——爬取豆瓣電影Top250代碼實(shí)例
利用python爬取豆瓣電影Top250的相關(guān)信息,包括電影詳情鏈接,圖片鏈接,影片中文名,影片外國(guó)名,評(píng)分,評(píng)價(jià)數(shù),概況,導(dǎo)演,主演,年份,地區(qū),類(lèi)別這12項(xiàng)內(nèi)容,然后將爬取的信息寫(xiě)入Excel表中。基本上爬取結(jié)果還是挺好的。具體代碼如下:
#!/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ū),類(lèi)別 findBd=re.compile(r'<p class="">(.*?)</p>',re.S) #去掉無(wú)關(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è)中文名,沒(méi)有外國(guó)名 if(len(titles)==2): ctitle=titles[0] data.append(ctitle)#添加中文片名 otitle=titles[1].replace(" / ","")#去掉無(wú)關(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) #可能沒(méi)有概況 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)而沒(méi)有 if(len(data)!=12): data.insert(8,' ')#留空 datalist.append(data) return datalist #將相關(guān)數(shù)據(jù)寫(xiě)入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ū)','類(lèi)別') 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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
python爬蟲(chóng)之bs4數(shù)據(jù)解析
這篇文章主要介紹了python爬蟲(chóng)之bs4數(shù)據(jù)解析,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python爬蟲(chóng)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04用python給自己做一款小說(shuō)閱讀器過(guò)程詳解
這篇文章主要介紹了用python給自己做一款小說(shuō)閱讀器過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-0714個(gè)用Python實(shí)現(xiàn)的Excel常用操作總結(jié)
自從學(xué)了Python后就逼迫自己不用Excel,所有操作用Python實(shí)現(xiàn)。目的是鞏固Python,與增強(qiáng)數(shù)據(jù)處理能力。本文為大家總結(jié)了14個(gè)用Python實(shí)現(xiàn)的Excel常用操作,需要的可以參考一下2022-06-065分鐘快速掌握Python定時(shí)任務(wù)框架的實(shí)現(xiàn)
這篇文章主要介紹了5分鐘快速掌握 Python 定時(shí)任務(wù)框架,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Python?LeNet網(wǎng)絡(luò)詳解及pytorch實(shí)現(xiàn)
LeNet主要用來(lái)進(jìn)行手寫(xiě)字符的識(shí)別與分類(lèi),并在美國(guó)的銀行中投入了使用。本文主要為大家詳細(xì)介紹了LetNet以及通過(guò)pytorch實(shí)現(xiàn)LetNet,感興趣的小伙伴可以學(xué)習(xí)一下2021-11-11python筆記_將循環(huán)內(nèi)容在一行輸出的方法
今天小編就為大家分享一篇python筆記_將循環(huán)內(nèi)容在一行輸出的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Python實(shí)現(xiàn)爬蟲(chóng)IP負(fù)載均衡和高可用集群的示例代碼
做大型爬蟲(chóng)項(xiàng)目經(jīng)常遇到請(qǐng)求頻率過(guò)高的問(wèn)題,這里需要說(shuō)的是使用爬蟲(chóng)IP可以提高抓取效率,本文主要介紹了Python實(shí)現(xiàn)爬蟲(chóng)IP負(fù)載均衡和高可用集群的示例代碼,感興趣的可以了解一下2023-12-12