Python爬蟲(chóng)入門(mén)案例之爬取去哪兒旅游景點(diǎn)攻略以及可視化分析
知識(shí)點(diǎn)
- requests 發(fā)送網(wǎng)絡(luò)請(qǐng)求
- parsel 解析數(shù)據(jù)
- csv 保存數(shù)據(jù)
第三方庫(kù)
- requests >>> pip install requests
- parsel >>> pip install parsel
開(kāi)發(fā)環(huán)境:
- 版 本: python 3.8
- 編輯器:pycharm 2021.2
【付費(fèi)VIP完整版】只要看了就能學(xué)會(huì)的教程,80集Python基礎(chǔ)入門(mén)視頻教學(xué)
爬蟲(chóng)程序
導(dǎo)入模塊
# 發(fā)送網(wǎng)絡(luò)請(qǐng)求的模塊 import requests # 解析數(shù)據(jù)的模塊 import parsel import csv import time import random
發(fā)送請(qǐng)求
url = f'https://travel.qunar.com/travelbook/list.htm?page=1&order=hot_heat' # <Response [200]>: 告訴我們 請(qǐng)求成功了 response = requests.get(url)
獲取數(shù)據(jù)(網(wǎng)頁(yè)源代碼)
html_data = response.text
解析網(wǎng)頁(yè)(re正則表達(dá)式,css選擇器,xpath,bs4/六年沒(méi)更新了,json)
# html_data: 字符串 # 我們現(xiàn)在要把這個(gè)字符串 變成一個(gè)對(duì)象 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)
向詳情頁(yè)網(wǎng)站發(fā)送請(qǐng)求(get,post)
# https://travel.qunar.com/travelbook/note/7701502 response_1 = requests.get(url_1).text
解析網(wǎng)頁(yè)
selector_1 = parsel.Selector(response_1) # :nth-child(): 偽類(lèi)選擇器 # ::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) # 寫(xiě)入數(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及對(duì)應(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)名稱(chēng)', 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)名稱(chēng)', 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)休延長(zhǎng)至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ā)到朋友圈;工作之后就沒(méi)有了寒暑假,所以利用周末來(lái)一次短途旅行就成為了大多數(shù)人的首選。
到此這篇關(guān)于Python爬蟲(chóng)入門(mén)案例之爬取去哪兒旅游景點(diǎn)攻略以及可視化分析的文章就介紹到這了,更多相關(guān)Python 爬取去哪兒內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python7個(gè)爬蟲(chóng)小案例詳解(附源碼)下篇
- Python7個(gè)爬蟲(chóng)小案例詳解(附源碼)中篇
- Python7個(gè)爬蟲(chóng)小案例詳解(附源碼)上篇
- 利用Python爬蟲(chóng)爬取金融期貨數(shù)據(jù)的案例分析
- Python爬蟲(chóng)采集Tripadvisor數(shù)據(jù)案例實(shí)現(xiàn)
- Python?Ajax爬蟲(chóng)案例分享
- Python爬蟲(chóng)入門(mén)案例之爬取二手房源數(shù)據(jù)
- Python爬蟲(chóng)入門(mén)案例之回車(chē)桌面壁紙網(wǎng)美女圖片采集
- Python爬蟲(chóng)之Scrapy環(huán)境搭建案例教程
- 用Python爬蟲(chóng)破解滑動(dòng)驗(yàn)證碼的案例解析
- python爬蟲(chóng)系列網(wǎng)絡(luò)請(qǐng)求案例詳解
- python爬蟲(chóng)破解字體加密案例詳解
- python爬蟲(chóng)線程池案例詳解(梨視頻短視頻爬取)
- python爬蟲(chóng)scrapy框架的梨視頻案例解析
- python爬蟲(chóng)利器之requests庫(kù)的用法(超全面的爬取網(wǎng)頁(yè)案例)
- Python爬蟲(chóng)實(shí)戰(zhàn)案例之爬取喜馬拉雅音頻數(shù)據(jù)詳解
- Python爬蟲(chóng)Scrapy框架CrawlSpider原理及使用案例
- Python爬蟲(chóng)之對(duì)CSDN榜單進(jìn)行分析
相關(guān)文章
基于Python實(shí)現(xiàn)一個(gè)簡(jiǎn)易的數(shù)據(jù)管理系統(tǒng)
為了方便的實(shí)現(xiàn)記錄數(shù)據(jù)、修改數(shù)據(jù)沒(méi)有精力去做一個(gè)完整的系統(tǒng)去管理數(shù)據(jù)。因此,在python的控制臺(tái)直接實(shí)現(xiàn)一個(gè)簡(jiǎn)易的數(shù)據(jù)管理系統(tǒng),包括數(shù)據(jù)的增刪改查等等。感興趣的可以跟隨小編一起學(xué)習(xí)一下2021-12-12Python BeautifulSoup中文亂碼問(wèn)題的2種解決方法
這篇文章主要介紹了Python BeautifulSoup中文亂碼問(wèn)題的2種解決方法,需要的朋友可以參考下2014-04-04Python+matplotlib實(shí)現(xiàn)填充螺旋實(shí)例
這篇文章主要介紹了Python+matplotlib實(shí)現(xiàn)填充螺旋實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01python nmap實(shí)現(xiàn)端口掃描器教程
這篇文章主要為大家詳細(xì)介紹了python nmap實(shí)現(xiàn)端口掃描器教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換
這篇文章主要介紹了TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09