Python爬取豆瓣电影top
Python爬取豆瓣电影top250
下面以四种方法去解析数据,前面三种以插件库来解析,第四种以正则表达式去解析。
爬取信息:名称 评分 小评

结果显示

使用xpath解析数据
#python 使用xpath解析数据
#查询豆瓣top250电影
#获取信息:名称 评分 短语
#关于xpath语法:https://www.w3school.com.cn/xpath/xpath_syntax.asp from lxml import etree
import time
import requests
import os #创建文件
t = time.strftime('%Y-%m-%d', time.localtime()) # 将指定格式的当前时间以字符串输出
suffix = ".txt"
newfile ="./log/xpath_"+ t + suffix
if not os.path.exists(newfile):
f = open(newfile, 'w',encoding="utf-8")
f.close() #打开文件,准备写入信息
f = open(newfile, 'w',encoding="utf-8")
start=0
while start<250:
# 查询top250电影,第页25条,取10页
r=requests.get("https://movie.douban.com/top250?start="+str(start) +"&filter=")
el=etree.HTML(r.content)
r.close() #解析内容
el_items=el.xpath('//div[@class="item"]') for item in el_items:
#当获取子项信息时,xpath开头不能以“/”或“//”开头,“//”是查询整个html。开始一定要指当前子项,后面可以使用“/”或“//”来搜索
title=item.xpath('div//span[@class="title"][1]/text()')[0] #标题
rating_num=item.xpath('div//span[@class="rating_num"][1]/text()')[0]#评分
# 小评可能不存在,在此加判断
inq=item.xpath('div//span[@class="inq"][1]/text()')#小评
inq_str=""
if len(inq)>0:
inq_str=inq[0] #写入文件
f.write(str(title).strip().ljust(20,'—')+str(rating_num).strip().ljust(20,' ')+">"+str(inq_str).strip().ljust(50,' ')+"\n")
start+=25
#最后关闭文件
f.close()
print("the end")
使用pyquery解析数据
#python 使用pyquery解析数据
#查询豆瓣top250电影
#获取信息:名称 评分 短语
#关于pyquery语法:https://pyquery.readthedocs.io/en/latest/pseudo_classes.html
from pyquery import PyQuery as pq
import time
import requests
import os #创建文件
t = time.strftime('%Y-%m-%d', time.localtime()) # 将指定格式的当前时间以字符串输出
suffix = ".txt"
newfile ="./log/pyquery_"+ t + suffix
if not os.path.exists(newfile):
f = open(newfile, 'w',encoding="utf-8")
f.close() #打开文件,准备写入信息
f = open(newfile, 'w',encoding="utf-8")
start=0
while start<250:
#查询top250电影,第页25条,取10页
r = requests.get("https://movie.douban.com/top250?start=" + str(start) + "&filter=")
d=pq(r.content)
r.close()
items=d('.item')
for item in items:
item_d=pq(item)#重新加载每一项html,为下面取出信息
title= item_d.find(".title:eq(0)").text()#名称
rating_num =item_d.find(".rating_num:eq(0)").text()# 评分
inq_str = item_d.find('.inq:eq(0)').text() # 小评 # 写入文件
f.write(str(title).strip().ljust(20,'—')+str(rating_num).strip().ljust(20,' ')+">"+str(inq_str).strip().ljust(50,' ')+"\n")
start+=25
#最后关闭文件
f.close()
print("the end")
使用BeaufifulSoup解析数据
#python 使用BeaufifulSoup解析数据
#查询豆瓣top250电影
#获取信息:名称 评分 短语
#关于语法:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
from bs4 import BeautifulSoup
import time
import requests
import os #创建文件
t = time.strftime('%Y-%m-%d', time.localtime()) # 将指定格式的当前时间以字符串输出
suffix = ".txt"
newfile ="./log/BeaufifulSoup_"+ t + suffix
if not os.path.exists(newfile):
f = open(newfile, 'w',encoding="utf-8")
f.close() #打开文件,准备写入信息
f = open(newfile, 'w',encoding="utf-8")
start=0
while start<250:
#查询top250电影,第页25条,取10页
r=requests.get("https://movie.douban.com/top250?start="+str(start) +"&filter=")
el=BeautifulSoup(r.content,"xml")
r.close()
items=el.find_all("div", class_="item")#获取一项电影信息 for item in items:
title=item.find_all(class_="title",limit=1)[0].get_text()#名称
rating_num=item.find_all('span',class_="rating_num",limit=1)[0].get_text() # 评分 # 小评可能不存在,在此加判断
inq = item.find_all('span',class_="inq",limit=1) # 小评
inq_str = ""
if len(inq) > 0:
inq_str = inq[0].get_text()
f.write(str(title).strip().ljust(20,'—')+str(rating_num).strip().ljust(20,' ')+">"+str(inq_str).strip().ljust(50,' ')+"\n")
#print(str(title).strip().ljust(20,'—')+str(rating_num).strip().ljust(20,' ')+">"+str(inq_str).strip().ljust(50,' ')+"\n")
start+=25
#最后关闭文件
f.close()
print("the end")
使用re正则匹配
#python 使用re正则匹配
#查询豆瓣top250电影
#获取信息:名称 评分 短语
import re
import time
import requests
import os
reg_items=re.compile('<li>[\r\n\s]+<div\s+class="item">[.\r\n\s\S]*?</li>')#每个电影
reg_title=re.compile('(?<=title">)[^<]+')#电影名称
reg_rating_num=re.compile('(?<=property="v:average">)[^<]+')#评分
reg_inq=re.compile('(?<=class="inq">)[^<]+')#小评 #创建文件
t = time.strftime('%Y-%m-%d', time.localtime()) # 将指定格式的当前时间以字符串输出
suffix = ".txt"
newfile ="./log/re_"+ t + suffix
if not os.path.exists(newfile):
f = open(newfile, 'w',encoding="utf-8")
f.close() #打开文件,准备写入信息
f = open(newfile, 'w',encoding="utf-8")
start=0
while start<250:
#查询top250电影,第页25条,取10页
r = requests.get("https://movie.douban.com/top250?start=" + str(start) + "&filter=")
html=str(r.content,encoding = "utf-8")
r.close()
maths= reg_items.findall(html)
for item in maths:
re_title=reg_title.search(item)
title=re_title.group(0)
re_rating_num=reg_rating_num.search(item)
rating_num=re_rating_num.group(0)
inq_str=""
#小评可能不存在,在此加判断
re_inq=reg_inq.search(item)
if re_inq!=None:
inq_str=re_inq.group(0)
f.write(str(title).strip().ljust(20, '—') + str(rating_num).strip().ljust(20, ' ') + ">" + str( inq_str).strip().ljust(50, ' ') + "\n")
#print(str(title).strip().ljust(20,'—')+str(rating_num).strip().ljust(20,' ')+">"+str(inq_str).strip().ljust(50,' ')+"\n")
start+=25
#最后关闭文件
f.close()
print("the end")
为毛要这么方法去解析?从众多方式做一个比较,那种方式有优势,解析起来更方便。以后需要解析的时候,从中选择最优的。
来源:https://www.cnblogs.com/cai-niao/p/11372087.html 黑白记忆
Python爬取豆瓣电影top的更多相关文章
- 用python爬取豆瓣电影Top 250
首先,打开豆瓣电影Top 250,然后进行网页分析.找到它的Host和User-agent,并保存下来. 然后,我们通过翻页,查看各页面的url,发现规律: 第一页:https://movie.dou ...
- 爬虫系列1:Requests+Xpath 爬取豆瓣电影TOP
爬虫1:Requests+Xpath 爬取豆瓣电影TOP [抓取]:参考前文 爬虫系列1:https://www.cnblogs.com/yizhiamumu/p/9451093.html [分页]: ...
- 爬取豆瓣电影TOP 250的电影存储到mongodb中
爬取豆瓣电影TOP 250的电影存储到mongodb中 1.创建项目sp1 PS D:\scrapy> scrapy.exe startproject douban 2.创建一个爬虫 PS D: ...
- 利用Python爬取豆瓣电影
目标:使用Python爬取豆瓣电影并保存MongoDB数据库中 我们先来看一下通过浏览器的方式来筛选某些特定的电影: 我们把URL来复制出来分析分析: https://movie.douban.com ...
- 爬虫——正则表达式爬取豆瓣电影TOP前250的中英文名
正则表达式爬取豆瓣电影TOP前250的中英文名 1.首先要实现网页的数据的爬取.新建test.py文件 test.py 1 import requests 2 3 def get_Html_text( ...
- Python开发爬虫之静态网页抓取篇:爬取“豆瓣电影 Top 250”电影数据
所谓静态页面是指纯粹的HTML格式的页面,这样的页面在浏览器中展示的内容都在HTML源码中. 目标:爬取豆瓣电影TOP250的所有电影名称,网址为:https://movie.douban.com/t ...
- python 爬取豆瓣电影评论,并进行词云展示及出现的问题解决办法
本文旨在提供爬取豆瓣电影<我不是药神>评论和词云展示的代码样例 1.分析URL 2.爬取前10页评论 3.进行词云展示 1.分析URL 我不是药神 短评 第一页url https://mo ...
- python爬取豆瓣电影信息数据
题外话+ 大家好啊,最近自己在做一个属于自己的博客网站(准备辞职回家养老了,明年再战)在家里 琐事也很多, 加上自己 一回到家就懒了(主要是家里冷啊! 广东十几度,老家几度,躲在被窝瑟瑟发抖,) 由于 ...
- python 爬取豆瓣电影短评并wordcloud生成词云图
最近学到数据可视化到了词云图,正好学到爬虫,各种爬网站 [实验名称] 爬取豆瓣电影<千与千寻>的评论并生成词云 1. 利用爬虫获得电影评论的文本数据 2. 处理文本数据生成词云图 第一步, ...
随机推荐
- windows10 启动安卓模拟器会蓝屏的解决方案
最近突然想用win10装个安卓模拟器玩游戏,然后提示vt被占用. 查了一下,了解到在windows 10 系统上,我们会用vmware,virtual box ,hyper-v,安卓模拟器,360安全 ...
- mybatis批量更新出现he error occurred while setting parameters
当你更新一条时,不会发生问题,但是执行多条就出现了错误原因是mysql 配置jdbc:driver 应该添加&allowMultiQueries=trueurl:jdbc:mysql://lo ...
- Kubernetes 有状态与无状态介绍
Kubernetes 有状态与无状态介绍 无状态:deployment - 认为所有pod都是一样的,不具备与其他实例有不同的关系. - 没有顺序的要求. - 不用考虑再哪个Node运行. - 随意扩 ...
- 配置 ASP.NET Core 请求(Request)处理管道
配置 ASP.NET Core 请求(Request)处理管道 在本节中,我们将讨论使用中间件组件为 asp.net core 应用程序配置请求处理管道. 作为应用程序启动的一部分,我们要在Confi ...
- 纯 JS 设置文本框的默认提示
HTML5 中有个新特性叫 placeholder,一般用它来描述输入字段的预期值,适用于 text.search.password 等类型的 input 以及 textarea.示例如下: < ...
- 你不知道的Golang map
在开发过程中,map是必不可少的数据结构,在Golang中,使用map或多或少会遇到与其他语言不一样的体验,比如访问不存在的元素会返回其类型的空值.map的大小究竟是多少,为什么会报"can ...
- Python - 条件控制、循环语句 - 第十二天
Python 条件控制.循环语句 end 关键字 关键字end可以用于将结果输出到同一行,或者在输出的末尾添加不同的字符,实例如下: Python 条件语句是通过一条或多条语句的执行结果(True 或 ...
- React的jsx语法,详细介绍和使用方法!
jsx语法 一种混合使用html及javascript语法的代码 在js中 遇到<xx>即开始html语法 遇到</xx>则结束html语法 恢复成js语法 例如: let D ...
- gsoap工具生成wsdl接口 注意事项
wsdl是通过wsdl文件作为不同应用的通信接口,所以如何生成wsdl语言很重要,但是很多时候我们发现自己编写的头文件通过gsoap工具soapcpp2.exe从头文件中无法正常生成对应的wsdl语言 ...
- SQLi-LABS Page-2 (Adv Injections) Less27-Less29
Less-27 GET - Error Based- All your UNION and select belong to us 过滤了union 和select的报错注入 查看源码: 使用%09 ...