欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python爬蟲(chóng)入門(mén)案例之爬取去哪兒旅游景點(diǎn)攻略以及可視化分析

 更新時(shí)間:2021年10月15日 10:58:59   作者:松鼠愛(ài)吃餅干  
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,學(xué)的扎不扎實(shí)要通過(guò)實(shí)戰(zhàn)才能看出來(lái),本篇文章手把手帶你爬取去哪兒平臺(tái)的旅游景點(diǎn)攻略并進(jìn)行可視化分析,大家可以在過(guò)程中查缺補(bǔ)漏,看看自己掌握程度怎么樣

知識(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é)

點(diǎn)這里即可免費(fèi)在線觀看

爬蟲(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于Python實(shí)現(xiàn)一個(gè)簡(jiǎn)易的數(shù)據(jù)管理系統(tǒng)

    基于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-12
  • Python BeautifulSoup中文亂碼問(wèn)題的2種解決方法

    Python BeautifulSoup中文亂碼問(wèn)題的2種解決方法

    這篇文章主要介紹了Python BeautifulSoup中文亂碼問(wèn)題的2種解決方法,需要的朋友可以參考下
    2014-04-04
  • python使用scrapy解析js示例

    python使用scrapy解析js示例

    這篇文章主要介紹了python使用scrapy解析js的示例,大家參考使用吧
    2014-01-01
  • Python+matplotlib實(shí)現(xiàn)填充螺旋實(shí)例

    Python+matplotlib實(shí)現(xiàn)填充螺旋實(shí)例

    這篇文章主要介紹了Python+matplotlib實(shí)現(xiàn)填充螺旋實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Python視頻剪輯Moviepy庫(kù)使用教程

    Python視頻剪輯Moviepy庫(kù)使用教程

    這篇文章主要為大家介紹了Python視頻剪輯Moviepy庫(kù)使用教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Pycharm配置PyQt5環(huán)境的教程

    Pycharm配置PyQt5環(huán)境的教程

    這篇文章主要介紹了Pycharm配置PyQt5環(huán)境的教程,本文通過(guò)圖文實(shí)例詳解給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • python nmap實(shí)現(xiàn)端口掃描器教程

    python nmap實(shí)現(xiàn)端口掃描器教程

    這篇文章主要為大家詳細(xì)介紹了python nmap實(shí)現(xiàn)端口掃描器教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換

    TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換

    這篇文章主要介紹了TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • python異步IO的項(xiàng)目實(shí)踐

    python異步IO的項(xiàng)目實(shí)踐

    本文主要介紹了python異步IO的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Python并行分布式框架Celery詳解

    Python并行分布式框架Celery詳解

    今天小編就為大家分享一篇關(guān)于Python并行分布式框架Celery詳解的文章,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10

最新評(píng)論