import parsel >>> pip install parsel
import requests >>> pip install requests
import csv
https://movie.douban.com/subject/35267208/comments?limit=20&status=P&sort=new_score
https://movie.douban.com/subject/35267208/comments?limit=20&status=P&sort=new_score
https://movie.douban.com/subject/35267208/comments?limit=20&status=P&sort=new_score
返回
表示请求成功
# 请求链接
url = f'https://movie.douban.com/subject/35267224/comments?start=20&limit=20&status=P&sort=new_score'
# 伪装模拟
headers = {
# User-Agent 用户代理, 表示浏览器基本身份标识
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
# 发送请求
response = requests.get(url=url, headers=headers)
print(response)
解析方法:
把获取下来html字符串数据
, 转成可解析对象
提取具体数据内容
.comment-info a --> 定位class类名为comment-info下面a标签
a::text --> 提取a标签里面文本
get() --> 获取第一个标签内容
attr() --> 提取属性
selector = parsel.Selector(response.text)
# 第一次提取, 所有div标签
divs = selector.css('div.comment-item')
# for循环遍历, 把列表里面元素一个一个提取出来
for div in divs:
name = div.css('.comment-info a::text').get() # 昵称
rating = div.css('.rating::attr(title)').get() # 推荐
date = div.css('.comment-time::attr(title)').get() # 时间
area = div.css('.comment-location::text').get() # 地区
votes = div.css('.votes::text').get() # 有用
short = div.css('.short::text').get().replace('\n', '') # 评论
# 数据存字典里面
dit = {
'昵称': name,
'推荐': rating,
'时间': date,
'地区': area,
'有用': votes,
'评论': short,
}
# 写入数据
print(name, rating, date, area, votes, short)
data.csv --> 文件名
mode=a --> 保存方式 追加保存
encoding=‘utf-8’ --> 编码格式
newline --> 换行符
f --> 文件对象
f = open('data10.csv', mode='a', encoding='utf-8-sig', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
'昵称',
'推荐',
'时间',
'地区',
'有用',
'评论',
])
# 写入表头
csv_writer.writeheader()
import pandas as pd
import jieba
import wordcloud
df = pd.read_csv('data10.csv')
df.head()
import pyecharts.options as opts
from pyecharts.charts import Pie
data_pair = [list(z) for z in zip(evaluate_type, evaluate_num)]
data_pair.sort(key=lambda x: x[1])
c = (
Pie(init_opts=opts.InitOpts(bg_color="#2c343c"))
.add(
series_name="豆瓣影评",
data_pair=data_pair,
rosetype="radius",
radius="55%",
center=["50%", "50%"],
label_opts=opts.LabelOpts(is_show=False, position="center"),
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="推荐分布",
pos_left="center",
pos_top="20",
title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
),
legend_opts=opts.LegendOpts(is_show=False),
)
.set_series_opts(
tooltip_opts=opts.TooltipOpts(
trigger="item", formatter="{a}
{b}: {c} ({d}%)"
),
label_opts=opts.LabelOpts(color="rgba(255, 255, 255, 0.3)"),
)
)
c.render_notebook()
import pyecharts.options as opts
from pyecharts.charts import Pie
data_pair = [list(z) for z in zip(area_type, area_num)]
data_pair.sort(key=lambda x: x[1])
d = (
Pie(init_opts=opts.InitOpts(bg_color="#2c343c"))
.add(
series_name="豆瓣影评",
data_pair=data_pair,
rosetype="radius",
radius="55%",
center=["50%", "50%"],
label_opts=opts.LabelOpts(is_show=False, position="center"),
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="地区分布",
pos_left="center",
pos_top="20",
title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
),
legend_opts=opts.LegendOpts(is_show=False),
)
.set_series_opts(
tooltip_opts=opts.TooltipOpts(
trigger="item", formatter="{a}
{b}: {c} ({d}%)"
),
label_opts=opts.LabelOpts(color="rgba(255, 255, 255, 0.3)"),
)
)
d.render_notebook()
适合练手的25个Python案例源码分享,总有一个你想要的