一、jupyter和pyecharts下载和打开
因为我是用的pycharm,所以我直接在pycharm项目终端中下载pip install jupyter,pip install pyecharts
在你下载的项目路径中输入jupyter notebook
之后会进入页面
Jupyter 具体使用参考这个链接:Jupyter Notebook基本使用_jupyter notebook有哪些优点和缺点?-CSDN博客
二、jupyter 利用pyecharts常见问题
最常见的问题是不出图
解决方法:
在导入包时候,导入下面的代码:
from pyecharts.globals import CurrentConfig,NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
接着写代码
一定要另起一行写展示代码
三、爬取懂车帝数据并进行数据分析详细代码
爬虫代码:
import csv
import requests
f = open('懂车帝.csv',mode='w',encoding='utf-8',newline='')
csv_writer = csv.writer(f)
csv_writer.writerow(['车辆名称','系列名称','品牌','城市','过户次数','售价','官方指导价','年份','行驶里程','汽车源类型','认证'])
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
url = 'https://www.dongchedi.com/motor/pc/sh/sh_sku_list?aid=1839&app_name=auto_web_pc'
for page in range(1,21):
data = {
'sh_city_name':' 全国',
'page': page,
'limit': '20',
}
response = requests.post(url=url,json=data,headers=headers)
json_data = response.json()['data']['search_sh_sku_info_list']
print(f'正在打印第{page}页数据')
for item in json_data:
car_name = item['car_name']
series_name = item['series_name']
brand_name = item['brand_name']
car_source_city_name = item['car_source_city_name']
transfer_cnt = item['transfer_cnt']
sh_price = item['sh_price']
official_price = item['official_price']
productionTime = item['sub_title'].split('|')[0]
car_source_type = item['car_source_type']
authentication_method = item['authentication_method']
try:
mileage = item['sub_title'].split('|')[1]
except:
mileage = '无数据'
csv_writer.writerow([car_name, series_name, brand_name, car_source_city_name, transfer_cnt, sh_price, official_price,productionTime, mileage, car_source_type, authentication_method])
分析代码:
import pandas as pd
# 配置pyecharts的代码
# 读取数据
df = pd.read_csv('懂车帝.csv')
# 显示前五行
df.head()
info = df['品牌'].value_counts().index.to_list() # x轴的内容,分类型
num = df['品牌'].value_counts().to_list() # y轴的数据
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
from pyecharts.globals import CurrentConfig,NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
c = (
Pie()
.add(
"",
[
list(z)
for z in zip(
# 传入的x和y轴数据
info,
num,
)
],
center=["40%", "50%"],
)
.set_global_opts(
# 设置标题
title_opts=opts.TitleOpts(title="二手车数据类型分布"),
legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical"),
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
# 把可视化图保存为html文件
#.render("pie_scroll_legend.html")
)
c.load_javascript()
c.render_notebook()
结果展示:
四、pyecharts官方示例代码链接:Document
官方链接:pyecharts - A Python Echarts Plotting Library built with love.