Python简单爬取旅游景点数据并绘制图标
2019-10-10 本文已影响0人
EmpathyBasic
参考https://mp.weixin.qq.com/s/s-d5VxSbYKd1xmvS2XrvWQ
网络请求requests
数据保存pandas
绘制图标pyecharts
import math
import os
import time
import pandas
import requests
from pyecharts import options as opts
from pyecharts.charts import Bar
PATH = 'travel.xlsx'
if os.path.exists(PATH):
os.remove(PATH)
total_data = []
# 爬取数据
for i in range(18):
print('正在爬取第', str(i + 1), '页数据')
resp = requests.get(
'https://piao.qunar.com/ticket/list.json?keyword=%E5%9B%BD%E5%BA%86%E6%97%85%E6%B8%B8%E6%99%AF%E7%82%B9®ion'
'=&from=mps_search_suggest&page=' + str(i + 1))
data = resp.json()['data']['sightList']
print(data)
total_data += data
time.sleep(1)
# 保存到Excel
df = pandas.DataFrame(total_data)
df.to_excel(PATH,
columns=['sightId', 'sightName', 'star', 'score', 'qunarPrice', 'saleCount', 'districts', 'point',
'intro'])
# 分析数据,增加销售额字段,并根据销售额排序
df = pandas.read_excel(PATH)
sale_list = []
for index, Series in df.iterrows():
sale = Series['qunarPrice'] * Series['saleCount']
if math.isnan(sale):
sale = 0
sale_list.append(sale)
print(Series['sightName'], '销售额', sale)
df['sale'] = sale_list
df = df.sort_values('sale')
# 绘制图标
bar = (
Bar()
.add_xaxis(df['sightName'][-20:].tolist())
.add_yaxis('销售额', df['sale'][-20:].tolist())
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position='right'))
.set_global_opts(title_opts=opts.TitleOpts(title="旅游景点销售情况"))
)
bar.render()
excel.png
echars.png