python爬取豆瓣评分排行榜(Python爬虫——爬取豆瓣电影Top250代码实例)
类别:脚本大全 浏览量:380
时间:2021-10-18 11:10:57 python爬取豆瓣评分排行榜
Python爬虫——爬取豆瓣电影Top250代码实例利用python爬取豆瓣电影top250的相关信息,包括电影详情链接,图片链接,影片中文名,影片外国名,评分,评价数,概况,导演,主演,年份,地区,类别这12项内容,然后将爬取的信息写入excel表中。基本上爬取结果还是挺好的。具体代码如下:
|
#!/usr/bin/python #-*- coding: utf-8 -*- import sys reload (sys) sys.setdefaultencoding( 'utf8' ) from bs4 import beautifulsoup import re import urllib2 import xlwt #得到页面全部内容 def askurl(url): request = urllib2.request(url) #发送请求 try : response = urllib2.urlopen(request) #取得响应 html = response.read() #获取网页内容 #print html except urllib2.urlerror, e: if hasattr (e, "code" ): print e.code if hasattr (e, "reason" ): print e.reason return html #获取相关内容 def getdata(baseurl): findlink = re. compile (r '<a href="(.*?)" rel="external nofollow" >' ) #找到影片详情链接 findimgsrc = re. compile (r '<img.*src="(..jpg" alt="python爬取豆瓣评分排行榜(Python爬虫——爬取豆瓣电影Top250代码实例)" border="0" /> findtitle = re. compile (r '<span class="title">(.*)</span>' ) #找到片名 #找到评分 findrating = re. compile (r '<span class="rating_num" property="v:average">(.*)</span>' ) #找到评价人数 findjudge = re. compile (r '<span>(\d*)人评价</span>' ) #找到概况 findinq = re. compile (r '<span class="inq">(.*)</span>' ) #找到影片相关内容:导演,主演,年份,地区,类别 findbd = re. compile (r '<p class="">(.*?)</p>' ,re.s) #去掉无关内容 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( 'li' , class_ = 'item' ): #找到每一个影片项 data = [] item = str (item) #转换成字符串 #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( " / " ,"") #去掉无关符号 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) #添加评论人数 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! = ' ' : #去掉空白内容 data.append(s) #主演有可能因为导演内容太长而没有 if ( len (data)! = 12 ): data.insert( 8 , ' ' ) #留空 datalist.append(data) return datalist #将相关数据写入excel中 def savedata(datalist,savepath): book = xlwt.workbook(encoding = 'utf-8' ,style_compression = 0 ) sheet = book.add_sheet( '豆瓣电影top250' ,cell_overwrite_ok = true) col = ( '电影详情链接' , '图片链接' , '影片中文名' , '影片外国名' , '评分' , '评价数' , '概况' , '导演' , '主演' , '年份' , '地区' , '类别' ) 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]) #数据 book.save(savepath) #保存 def main(): baseurl = 'https://movie.douban.com/top250?start=' datalist = getdata(baseurl) savapath = u '豆瓣电影top250.xlsx' savedata(datalist,savapath) main() |
excel表部分内容如下:
以上所述是小编给大家介绍的python爬取豆瓣电影top250实例详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对开心学习网网站的支持!
原文链接:https://blog.csdn.net/Fighting_No1/article/details/50926008
您可能感兴趣
- python爬虫经典步骤(详解python爬虫系列之初识爬虫)
- python3爬虫代码(Python3爬楼梯算法示例)
- python爬虫入门代码(python爬虫基础教程:requests库二代码实例)
- python实现网络爬虫的步骤(Python实现的爬取小说爬虫功能示例)
- python3爬虫实例代码(python3通过selenium爬虫获取到dj商品的实例代码)
- python网页自动化selenium(python利用selenium进行浏览器爬虫)
- nginx服务器怎么屏蔽爬虫(nginx 防盗链防爬虫配置详解)
- python爬虫音乐代码(详解python selenium 爬取网易云音乐歌单名)
- nginx过滤器漏洞(Nginx反爬虫策略,防止UA抓取网站)
- python豆瓣电影爬虫课程设计报告(详解python 模拟豆瓣登录豆瓣6.0)
- python获取天气源(Python3爬虫之自动查询天气并实现语音播报)
- python 爬虫招聘(Python3获取拉勾网招聘信息的方法实例)
- python scrapy爬虫教程视频(详解python3 + Scrapy爬虫学习之创建项目)
- python爬取在线评论(Python爬虫实现爬取百度百科词条功能实例)
- python爬虫爬取知乎(详解用python写网络爬虫-爬取新浪微博评论)
- 如何用python爬取最新电影(使用python实现抓取腾讯视频所有电影的爬虫)
- 硕博期刊 SCI SSCI CSSCI分不清 一文带你看懂主流期刊分类(硕博期刊SCISSCI)
- 辱华品牌新百伦官宣新代言人IU,个别粉丝希望get爱豆同款(辱华品牌新百伦官宣新代言人IU)
- 巅峰时期被爆床照,曾被选国民最讨厌女星,IU不为人知的黑历史(巅峰时期被爆床照)
- 每天1万吨牛奶倒进下水道,美国大萧条一幕重现(每天1万吨牛奶倒进下水道)
- 如何看待美国数十万加仑牛奶倒下水道 历史又重演了(如何看待美国数十万加仑牛奶倒下水道)
- 历史惊人的相似,美国80万加仑牛奶倒入下水道,意味着什么(历史惊人的相似)
热门推荐
- docker是k8s 编排必备容器工具(Docker部署ELK7.3.0日志收集服务最佳实践)
- html5表格设计(Html5饼图绘制实现统计图的方法)
- laravel终止函数执行(laravel 输出最后执行sql 附:whereIn的使用方法)
- wdc真正的底层公链(LiteSpeed服务器用htaccess的防盗链代码)
- 用python怎么找工作(利用python如何在前程无忧高效投递简历)
- SQL Server将数据导出到SQL脚本文件
- sql server日志文件过大怎么办
- mysql怎么和sqlyog连接(Mysql桌面工具之SQLyog资源及激活使用方法告别黑白命令行)
- 为什么asp.net网站第一次加载慢
- docker部署spring boot(Spring Boot 打包上传Docker 仓库的详细步骤)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9