Python爬蟲——爬取豆瓣電影Top250代碼實(shí)例
利用python爬取豆瓣電影Top250的相關(guān)信息,包括電影詳情鏈接,圖片鏈接,影片中文名,影片外國名,評分,評價(jià)數(shù),概況,導(dǎo)演,主演,年份,地區(qū),類別這12項(xiàng)內(nèi)容,然后將爬取的信息寫入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 #得到頁面全部內(nèi)容 def askURL(url): request = urllib2.Request(url)#發(fā)送請求 try: response = urllib2.urlopen(request)#取得響應(yīng) html= response.read()#獲取網(wǎng)頁內(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>')#找到片名 #找到評分 findRating=re.compile(r'<span class="rating_num" property="v:average">(.*)</span>') #找到評價(jià)人數(shù) findJudge=re.compile(r'<span>(\d*)人評價(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'):#找到每一個影片項(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) #片名可能只有一個中文名,沒有外國名 if(len(titles)==2): ctitle=titles[0] data.append(ctitle)#添加中文片名 otitle=titles[1].replace(" / ","")#去掉無關(guān)符號 data.append(otitle)#添加外國片名 else: data.append(titles[0])#添加中文片名 data.append(' ')#留空 rating=re.findall(findRating,item)[0] data.append(rating)#添加評分 judgeNum=re.findall(findJudge,item)[0] data.append(judgeNum)#添加評論人數(shù) inq=re.findall(findInq,item) #可能沒有概況 if len(inq)!=0: inq=inq[0].replace("。","")#去掉句號 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)容太長而沒有 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=('電影詳情鏈接','圖片鏈接','影片中文名','影片外國名', '評分','評價(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í)例詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
14個用Python實(shí)現(xiàn)的Excel常用操作總結(jié)
自從學(xué)了Python后就逼迫自己不用Excel,所有操作用Python實(shí)現(xiàn)。目的是鞏固Python,與增強(qiáng)數(shù)據(jù)處理能力。本文為大家總結(jié)了14個用Python實(shí)現(xiàn)的Excel常用操作,需要的可以參考一下2022-06-065分鐘快速掌握Python定時(shí)任務(wù)框架的實(shí)現(xiàn)
這篇文章主要介紹了5分鐘快速掌握 Python 定時(shí)任務(wù)框架,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Python?LeNet網(wǎng)絡(luò)詳解及pytorch實(shí)現(xiàn)
LeNet主要用來進(jìn)行手寫字符的識別與分類,并在美國的銀行中投入了使用。本文主要為大家詳細(xì)介紹了LetNet以及通過pytorch實(shí)現(xiàn)LetNet,感興趣的小伙伴可以學(xué)習(xí)一下2021-11-11python筆記_將循環(huán)內(nèi)容在一行輸出的方法
今天小編就為大家分享一篇python筆記_將循環(huán)內(nèi)容在一行輸出的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python實(shí)現(xiàn)爬蟲IP負(fù)載均衡和高可用集群的示例代碼
做大型爬蟲項(xiàng)目經(jīng)常遇到請求頻率過高的問題,這里需要說的是使用爬蟲IP可以提高抓取效率,本文主要介紹了Python實(shí)現(xiàn)爬蟲IP負(fù)載均衡和高可用集群的示例代碼,感興趣的可以了解一下2023-12-12