Python爬蟲入門案例之爬取去哪兒旅游景點(diǎn)攻略以及可視化分析
知識(shí)點(diǎn)
- requests 發(fā)送網(wǎng)絡(luò)請求
- parsel 解析數(shù)據(jù)
- csv 保存數(shù)據(jù)
第三方庫
- requests >>> pip install requests
- parsel >>> pip install parsel
開發(fā)環(huán)境:
- 版 本: python 3.8
- 編輯器:pycharm 2021.2

【付費(fèi)VIP完整版】只要看了就能學(xué)會(huì)的教程,80集Python基礎(chǔ)入門視頻教學(xué)
點(diǎn)這里即可免費(fèi)在線觀看
爬蟲程序
導(dǎo)入模塊
# 發(fā)送網(wǎng)絡(luò)請求的模塊 import requests # 解析數(shù)據(jù)的模塊 import parsel import csv import time import random
發(fā)送請求
url = f'https://travel.qunar.com/travelbook/list.htm?page=1&order=hot_heat' # <Response [200]>: 告訴我們 請求成功了 response = requests.get(url)
獲取數(shù)據(jù)(網(wǎng)頁源代碼)
html_data = response.text
解析網(wǎng)頁(re正則表達(dá)式,css選擇器,xpath,bs4/六年沒更新了,json)
# html_data: 字符串
# 我們現(xiàn)在要把這個(gè)字符串 變成一個(gè)對象
selector = parsel.Selector(html_data)
# ::attr(href) url_list:列表
url_list = selector.css('.b_strategy_list li h2 a::attr(href)').getall()
for detail_url in url_list:
# 字符串的 替換方法
detail_id = detail_url.replace('/youji/', '')
url_1 = 'https://travel.qunar.com/travelbook/note/' + detail_id
print(url_1)
向詳情頁網(wǎng)站發(fā)送請求(get,post)
# https://travel.qunar.com/travelbook/note/7701502 response_1 = requests.get(url_1).text
解析網(wǎng)頁
selector_1 = parsel.Selector(response_1)
# :nth-child(): 偽類選擇器
# ::text 提取文本內(nèi)容
# * 代表所有
# 地點(diǎn)
title = selector_1.css('.b_crumb_cont *:nth-child(3)::text').get().replace('旅游攻略', '')
# 短評(píng)
comment = selector_1.css('.title.white::text').get()
# 出發(fā)日期
date = selector_1.css('#js_mainleft > div.b_foreword > ul > li.f_item.when > p > span.data::text').get()
# 天數(shù)
days = selector_1.css('#js_mainleft > div.b_foreword > ul > li.f_item.howlong > p > span.data::text').get()
# 人均消費(fèi)
money = selector_1.css('#js_mainleft > div.b_foreword > ul > li.f_item.howmuch > p > span.data::text').get()
# 人物
character = selector_1.css('#js_mainleft > div.b_foreword > ul > li.f_item.who > p > span.data::text').get()
# 玩法
play_list = selector_1.css('#js_mainleft > div.b_foreword > ul > li.f_item.how > p > span.data span::text').getall()
play = ' '.join(play_list)
# 瀏覽量
count = selector_1.css('.view_count::text').get()
print(title, comment, date, days, money, character, play, count)
保存數(shù)據(jù)
# 保存成csv
csv_qne = open('去哪兒.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.writer(csv_qne)
# 寫入數(shù)據(jù)
csv_writer.writerow(['地點(diǎn)', '短評(píng)', '出發(fā)時(shí)間', '天數(shù)', '人均消費(fèi)', '人物', '玩法', '瀏覽量'])

數(shù)據(jù)可視化
導(dǎo)入模塊
import pandas as pd from pyecharts.commons.utils import JsCode from pyecharts.charts import * from pyecharts import options as opts
導(dǎo)入數(shù)據(jù)
data = pd.read_csv('去哪兒_數(shù)分.csv')
data

旅游勝地Top10及對應(yīng)費(fèi)用
bar=(
Bar(init_opts=opts.InitOpts(height='500px',width='1000px',theme='dark'))
.add_xaxis(m2)
.add_yaxis(
'目的地Top10',
n2,
label_opts=opts.LabelOpts(is_show=True,position='top'),
itemstyle_opts=opts.ItemStyleOpts(
color=JsCode("""new echarts.graphic.LinearGradient(
0, 0, 0, 1,[{offset: 0,color: 'rgb(255,99,71)'}, {offset: 1,color: 'rgb(32,178,170)'}])
"""
)
)
)
.set_global_opts(
title_opts=opts.TitleOpts(
title='目的地Top10'),
xaxis_opts=opts.AxisOpts(name='景點(diǎn)名稱',
type_='category',
axislabel_opts=opts.LabelOpts(rotate=90),
),
yaxis_opts=opts.AxisOpts(
name='數(shù)量',
min_=0,
max_=120.0,
splitline_opts=opts.SplitLineOpts(is_show=True,linestyle_opts=opts.LineStyleOpts(type_='dash'))
),
tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross')
)
.set_series_opts(
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_='average',name='均值'),
opts.MarkLineItem(type_='max',name='最大值'),
opts.MarkLineItem(type_='min',name='最小值'),
]
)
)
)
bar.render_notebook()

bar=(
Bar(init_opts=opts.InitOpts(height='500px',width='1000px',theme='dark'))
.add_xaxis(loc)
.add_yaxis(
'人均費(fèi)用',
price_mean2,
label_opts=opts.LabelOpts(is_show=True,position='top'),
itemstyle_opts=opts.ItemStyleOpts(
color=JsCode("""new echarts.graphic.LinearGradient(
0, 0, 0, 1,[{offset: 0,color: 'rgb(255,99,71)'}, {offset: 1,color: 'rgb(32,178,170)'}])
"""
)
)
)
.set_global_opts(
title_opts=opts.TitleOpts(
title='各景點(diǎn)人均費(fèi)用'),
xaxis_opts=opts.AxisOpts(name='景點(diǎn)名稱',
type_='category',
axislabel_opts=opts.LabelOpts(rotate=90),
),
yaxis_opts=opts.AxisOpts(
name='數(shù)量',
min_=0,
max_=2000.0,
splitline_opts=opts.SplitLineOpts(is_show=True,linestyle_opts=opts.LineStyleOpts(type_='dash'))
),
tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross')
)
.set_series_opts(
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_='average',name='均值'),
opts.MarkLineItem(type_='max',name='最大值'),
opts.MarkLineItem(type_='min',name='最小值'),
]
)
)
)
bar.render_notebook()

出游方式分析
pie = (Pie(init_opts=opts.InitOpts(theme='dark', width='1000px', height='800px'))
.add("", [z for z in zip(m1,n1)],
radius=["40%", "65%"])
.set_global_opts(title_opts=opts.TitleOpts(title="去哪兒\n\n出游結(jié)伴方式", pos_left='center', pos_top='center',
title_textstyle_opts=opts.TextStyleOpts(
color='#FF6A6A', font_size=30, font_weight='bold'),
),visualmap_opts=opts.VisualMapOpts(is_show=False,
min_=38,
max_=641,
is_piecewise=False,
dimension=0,
range_color=['#9400D3', '#008afb', '#ffec4a', '#FFA500','#ce5777']),legend_opts=opts.LegendOpts(is_show=False, pos_top='5%'),)
.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}", font_size=12),tooltip_opts=opts.TooltipOpts(trigger="item", formatter=": {c}"),itemstyle_opts={"normal": {"barBorderRadius": [30, 30, 30, 30],'shadowBlur': 10,'shadowColor': 'rgba(0,191,255,0.5)','shadowOffsetY': 1,'opacity': 0.8
}
})
)
pie.render_notebook()

出游時(shí)間分析
line = (
Line()
.add_xaxis(m4.tolist())
.add_yaxis('',n4.tolist())
)
line.render_notebook()

2021年的旅游時(shí)間曲線大約在五月一號(hào)起伏最大,原因肯定是因?yàn)榧倨谡{(diào)休延長至4天,為了調(diào)整自己生活及工作的狀態(tài),很多人利用這個(gè)假期去旅行放松自己。
出游玩法分析
m5 = []
n5 = []
for i in range(20):
m5.append(list[i][0])
n5.append(list[i][1])
m5.reverse()
m6 = m5
n5.reverse()
n6 = n5
bar = (
Bar(init_opts=opts.InitOpts(theme='dark', width='1000px',height ='500px'))
.add_xaxis(m6)
.add_yaxis('', n6)
.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position='insideRight',font_style='italic'),
itemstyle_opts=opts.ItemStyleOpts(
color=JsCode("""new echarts.graphic.LinearGradient(1, 0, 0, 0,
[{
offset: 0,
color: 'rgb(255,99,71)'
}, {
offset: 1,
color: 'rgb(32,178,170)'
}])"""))
)
.set_global_opts(
title_opts=opts.TitleOpts(title="出游玩法分析"),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
legend_opts=opts.LegendOpts(is_show=True))
.reversal_axis()
)
bar.render_notebook()

“攝影”和“美食”可謂與旅行息息相關(guān),一次完整的旅行最不能缺的就是“攝影”,拍美食發(fā)到朋友圈、拍風(fēng)景發(fā)到朋友圈、拍完美的自己發(fā)到朋友圈;工作之后就沒有了寒暑假,所以利用周末來一次短途旅行就成為了大多數(shù)人的首選。
到此這篇關(guān)于Python爬蟲入門案例之爬取去哪兒旅游景點(diǎn)攻略以及可視化分析的文章就介紹到這了,更多相關(guān)Python 爬取去哪兒內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信