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

【付費VIP完整版】只要看了就能學會的教程,80集Python基礎入門視頻教學
爬蟲程序
導入模塊
# 發(fā)送網(wǎng)絡請求的模塊 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正則表達式,css選擇器,xpath,bs4/六年沒更新了,json)
# html_data: 字符串
# 我們現(xiàn)在要把這個字符串 變成一個對象
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)容
# * 代表所有
# 地點
title = selector_1.css('.b_crumb_cont *:nth-child(3)::text').get().replace('旅游攻略', '')
# 短評
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()
# 人均消費
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(['地點', '短評', '出發(fā)時間', '天數(shù)', '人均消費', '人物', '玩法', '瀏覽量'])

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

旅游勝地Top10及對應費用
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='景點名稱',
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(
'人均費用',
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='各景點人均費用'),
xaxis_opts=opts.AxisOpts(name='景點名稱',
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()

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

2021年的旅游時間曲線大約在五月一號起伏最大,原因肯定是因為假期調(diào)休延長至4天,為了調(diào)整自己生活及工作的狀態(tài),很多人利用這個假期去旅行放松自己。
出游玩法分析
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()

“攝影”和“美食”可謂與旅行息息相關,一次完整的旅行最不能缺的就是“攝影”,拍美食發(fā)到朋友圈、拍風景發(fā)到朋友圈、拍完美的自己發(fā)到朋友圈;工作之后就沒有了寒暑假,所以利用周末來一次短途旅行就成為了大多數(shù)人的首選。
到此這篇關于Python爬蟲入門案例之爬取去哪兒旅游景點攻略以及可視化分析的文章就介紹到這了,更多相關Python 爬取去哪兒內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Python7個爬蟲小案例詳解(附源碼)下篇
- Python7個爬蟲小案例詳解(附源碼)中篇
- Python7個爬蟲小案例詳解(附源碼)上篇
- 利用Python爬蟲爬取金融期貨數(shù)據(jù)的案例分析
- Python爬蟲采集Tripadvisor數(shù)據(jù)案例實現(xiàn)
- Python?Ajax爬蟲案例分享
- Python爬蟲入門案例之爬取二手房源數(shù)據(jù)
- Python爬蟲入門案例之回車桌面壁紙網(wǎng)美女圖片采集
- Python爬蟲之Scrapy環(huán)境搭建案例教程
- 用Python爬蟲破解滑動驗證碼的案例解析
- python爬蟲系列網(wǎng)絡請求案例詳解
- python爬蟲破解字體加密案例詳解
- python爬蟲線程池案例詳解(梨視頻短視頻爬取)
- python爬蟲scrapy框架的梨視頻案例解析
- python爬蟲利器之requests庫的用法(超全面的爬取網(wǎng)頁案例)
- Python爬蟲實戰(zhàn)案例之爬取喜馬拉雅音頻數(shù)據(jù)詳解
- Python爬蟲Scrapy框架CrawlSpider原理及使用案例
- Python爬蟲之對CSDN榜單進行分析
相關文章
Python pandas遍歷行數(shù)據(jù)的2種方法小結(jié)
pandas在數(shù)據(jù)處理過程中,除了對整列字段進行處理之外,有時還需求對每一行進行遍歷,本文就來介紹Python pandas遍歷行數(shù)據(jù)的2種方法小結(jié),感興趣的可以了解一下2024-03-03
Python3.0中普通方法、類方法和靜態(tài)方法的比較
本文主要講解了Python3中普通方法、類方法和靜態(tài)方法的區(qū)別與比較,費了作者可大的勁整理了,希望對大家有用2019-05-05
Python字符串操作strip()和split()方法詳解
這篇文章主要介紹了Python中的strip()和split()方法,講解了它們的使用場景和典型用法,并展示了一些示例代碼,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-03-03
判斷Threading.start新線程是否執(zhí)行完畢的實例
這篇文章主要介紹了判斷Threading.start新線程是否執(zhí)行完畢的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
使用PyCharm進行遠程開發(fā)和調(diào)試的實現(xiàn)
這篇文章主要介紹了使用PyCharm進行遠程開發(fā)和調(diào)試的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
Python中break語句和continue語句的用法講解
在Python中,break語句和continue語句一般用于循環(huán)語句中,這篇文章主要介紹了Python中break語句和continue語句的用法小結(jié),需要的朋友可以參考下2022-12-12

