Python爬取當(dāng)網(wǎng)書(shū)籍?dāng)?shù)據(jù)并數(shù)據(jù)可視化展示
一、開(kāi)發(fā)環(huán)境
- Python 3.8
- Pycharm 2021.2 專(zhuān)業(yè)版
二、模塊使用
csv 模塊 把爬取下來(lái)的數(shù)據(jù)保存表格里面的 內(nèi)置模塊requests >>> pip install requests 數(shù)據(jù)請(qǐng)求模塊parsel >>> pip install parsel 數(shù)據(jù)解析模塊 css選擇器去提取數(shù)據(jù)
三、爬蟲(chóng)代碼實(shí)現(xiàn)步驟
- 導(dǎo)入所需模塊
- 發(fā)送請(qǐng)求, 用
python代碼模擬瀏覽器發(fā)送請(qǐng)求 - 解析數(shù)據(jù), 提取我們想要數(shù)據(jù)內(nèi)容
- 多頁(yè)爬取
- 保存數(shù)據(jù), 保存
csv表格里面
1. 導(dǎo)入所需模塊
import requests ?# 數(shù)據(jù)請(qǐng)求模塊 第三方模塊 需要 pip install requests import parsel ?# 數(shù)據(jù)解析模塊 第三方模塊 需要 pip install parsel import csv ?# 保存csv表格數(shù)據(jù)模塊 內(nèi)置模塊 import time ?# 時(shí)間模塊
2. 發(fā)送請(qǐng)求, 用python代碼模擬瀏覽器發(fā)送請(qǐng)求
headers 請(qǐng)求頭 作用就是python代碼偽裝成瀏覽器 對(duì)于服務(wù)器發(fā)送請(qǐng)求
User-Agent 用戶(hù)代理 瀏覽器的基本身份標(biāo)識(shí)
標(biāo)題中無(wú)效的返回字符或前導(dǎo)空格:User-Agent 不要留有空格
通過(guò)requests模塊里面get請(qǐng)求方法,對(duì)于url地址發(fā)送請(qǐng)求,并且攜帶上面header請(qǐng)求頭參數(shù),最后用response變量接收返回?cái)?shù)據(jù)
url = f'http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-{page}'
# headers 請(qǐng)求頭 字典數(shù)據(jù)類(lèi)型
headers = {
? ? 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
}
response = requests.get(url=url, headers=headers)3. 解析數(shù)據(jù), 提取我們想要數(shù)據(jù)內(nèi)容
selector = parsel.Selector(response.text) ?# 對(duì)于獲取到的html字符串?dāng)?shù)據(jù)進(jìn)行轉(zhuǎn)換 selector 對(duì)象
# css選擇器 就是根據(jù)標(biāo)簽屬性提取相應(yīng)的數(shù)據(jù)
lis = selector.css('ul.bang_list li')
for li in lis:
? ? # .name 定位 class類(lèi)名name標(biāo)簽 a 標(biāo)簽 attr() 屬性選擇器 取a標(biāo)簽里面title屬性 get() 獲取數(shù)據(jù)
? ? title = li.css('.name a::attr(title)').get() ?# 書(shū)名
? ? # 獲取標(biāo)簽里面文本數(shù)據(jù) 直接text
? ? comment = li.css('.star a::text').get().replace('條評(píng)論', '') ?# 評(píng)論
? ? recommend = li.css('.star .tuijian::text').get().replace('推薦', '') ?# 推薦
? ? author = li.css('.publisher_info a:nth-child(1)::attr(title)').get() ?# 作者
? ? publish = li.css('div:nth-child(6) a::text').get() ?# 出版社
? ? price_n = li.css('.price .price_n::text').get() ?# 售價(jià)
? ? price_r = li.css('.price .price_r::text').get() ?# 原價(jià)
? ? price_s = li.css('.price .price_s::text').get() ?# 折扣
? ? price_e = li.css('.price .price_e .price_n::text').get() ?# 電子書(shū)價(jià)格
? ? href = li.css('.name a::attr(href)').get() ?# 詳情頁(yè)
? ? dit = {
? ? ? ? '書(shū)名': title,
? ? ? ? '評(píng)論數(shù)': comment,
? ? ? ? '推薦量': recommend,
? ? ? ? '作者': author,
? ? ? ? '出版社': publish,
? ? ? ? '售價(jià)': price_n,
? ? ? ? '原價(jià)': price_r,
? ? ? ? '折扣': price_s,
? ? ? ? '電子書(shū)價(jià)格': price_e,
? ? ? ? '詳情頁(yè)': href,
? ? }
? ? csv_writer.writerow(dit) ?# 數(shù)據(jù)保存到csv
? ? print(title, comment, recommend, author, publish, price_n, price_r, price_s, price_e, href, sep=' | ')4. 多頁(yè)爬取
for page in range(1, 26):
? ? # 字符串格式化方法
? ? print(f'正在爬取第{page}頁(yè)的數(shù)據(jù)內(nèi)容')
? ? time.sleep(1.5)
? ? url = f'http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-{page}'5. 保存數(shù)據(jù), 保存csv表格里面
# 創(chuàng)建打開(kāi)一個(gè)文件 進(jìn)行保存
f = open('當(dāng)當(dāng)圖書(shū).csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
? ? '書(shū)名',
? ? '評(píng)論數(shù)',
? ? '推薦量',
? ? '作者',
? ? '出版社',
? ? '售價(jià)',
? ? '原價(jià)',
? ? '折扣',
? ? '電子書(shū)價(jià)格',
? ? '詳情頁(yè)',
])
csv_writer.writeheader() ?# 寫(xiě)入表頭運(yùn)行代碼,效果如下圖:

四、數(shù)據(jù)可視化
1.導(dǎo)入所需模塊
import pandas as pd from pyecharts.charts import * from pyecharts.globals import ThemeType#設(shè)定主題 from pyecharts.commons.utils import JsCode import pyecharts.options as opts
2.導(dǎo)入數(shù)據(jù)
df = pd.read_csv('書(shū)籍信息.csv', encoding='utf-8', engine='python')
df.head()
3.可視化
書(shū)籍總體價(jià)格區(qū)間:
pie1 = (
? ? Pie(init_opts=opts.InitOpts(theme='dark',width='1000px',height='600px'))
? ??
? ? .add('', datas_pair_1, radius=['35%', '60%'])
? ? .set_series_opts(label_opts=opts.LabelOpts(formatter=":wppm3vysvbp%"))
? ? .set_global_opts(
? ? ? ? title_opts=opts.TitleOpts(
? ? ? ? ? ? title="當(dāng)當(dāng)網(wǎng)書(shū)籍\n\n原價(jià)價(jià)格區(qū)間",?
? ? ? ? ? ? pos_left='center',?
? ? ? ? ? ? pos_top='center',
? ? ? ? ? ? title_textstyle_opts=opts.TextStyleOpts(
? ? ? ? ? ? ? ? color='#F0F8FF',?
? ? ? ? ? ? ? ? font_size=20,?
? ? ? ? ? ? ? ? font_weight='bold'
? ? ? ? ? ? ),
? ? ? ? )
? ? )
? ? .set_colors(['#EF9050', '#3B7BA9', '#6FB27C', '#FFAF34', '#D8BFD8', '#00BFFF', '#7FFFAA'])
)
pie1.render_notebook()?
pie1 = (
? ? Pie(init_opts=opts.InitOpts(theme='dark',width='1000px',height='600px'))
? ??
? ? .add('', datas_pair_2, radius=['35%', '60%'])
? ? .set_series_opts(label_opts=opts.LabelOpts(formatter=":wppm3vysvbp%"))
? ? .set_global_opts(
? ? ? ? title_opts=opts.TitleOpts(
? ? ? ? ? ? title="當(dāng)當(dāng)網(wǎng)書(shū)籍\n\n售價(jià)價(jià)格區(qū)間",?
? ? ? ? ? ? pos_left='center',?
? ? ? ? ? ? pos_top='center',
? ? ? ? ? ? title_textstyle_opts=opts.TextStyleOpts(
? ? ? ? ? ? ? ? color='#F0F8FF',?
? ? ? ? ? ? ? ? font_size=20,?
? ? ? ? ? ? ? ? font_weight='bold'
? ? ? ? ? ? ),
? ? ? ? )
? ? )
? ? .set_colors(['#EF9050', '#3B7BA9', '#6FB27C', '#FFAF34', '#D8BFD8', '#00BFFF', '#7FFFAA'])
)
pie1.render_notebook()?
各個(gè)出版社書(shū)籍?dāng)?shù)量柱狀圖:
bar=(
? ? Bar(init_opts=opts.InitOpts(height='500px',width='1000px',theme='dark'))
? ? .add_xaxis(counts.index.tolist())
? ? .add_yaxis(
? ? ? ? '出版社書(shū)籍?dāng)?shù)量',
? ? ? ? counts.values.tolist(),
? ? ? ? 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='各個(gè)出版社書(shū)籍?dāng)?shù)量柱狀圖'),
? ? ? ? ? ? xaxis_opts=opts.AxisOpts(name='書(shū)籍名稱(chēng)',
? ? ? ? ? ? type_='category', ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? axislabel_opts=opts.LabelOpts(rotate=90),
? ? ? ? ),
? ? ? ? yaxis_opts=opts.AxisOpts(
? ? ? ? ? ? name='數(shù)量',
? ? ? ? ? ? min_=0,
? ? ? ? ? ? max_=29.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()書(shū)籍評(píng)論數(shù)最高Top20:
bar=(
? ? Bar(init_opts=opts.InitOpts(height='500px',width='1000px',theme='dark'))
? ? .add_xaxis(price_top.index.tolist())
? ? .add_yaxis(
? ? ? ? '書(shū)籍單價(jià)',
? ? ? ? price_top.values.tolist(),
? ? ? ? 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='單價(jià)最高的書(shū)籍詳細(xì)柱狀圖'),
? ? ? ? ? ? xaxis_opts=opts.AxisOpts(name='書(shū)籍名稱(chēng)',
? ? ? ? ? ? type_='category', ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? axislabel_opts=opts.LabelOpts(rotate=90),
? ? ? ? ),
? ? ? ? yaxis_opts=opts.AxisOpts(
? ? ? ? ? ? name='單價(jià)/元',
? ? ? ? ? ? min_=0,
? ? ? ? ? ? max_=1080.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()
到此這篇關(guān)于Python爬取當(dāng)網(wǎng)書(shū)籍?dāng)?shù)據(jù)并數(shù)據(jù)可視化展示的文章就介紹到這了,更多相關(guān)Python爬取當(dāng)網(wǎng)書(shū)籍?dāng)?shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中淺復(fù)制copy與深復(fù)制deepcopy
本文主要介紹了python中淺復(fù)制copy與深復(fù)制deepcopy,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Python模糊查詢(xún)本地文件夾去除文件后綴的實(shí)例(7行代碼)
下面小編就為大家?guī)?lái)一篇Python模糊查詢(xún)本地文件夾去除文件后綴的實(shí)例(7行代碼) 。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
python docx的超鏈接網(wǎng)址和鏈接文本操作
這篇文章主要介紹了python docx的超鏈接網(wǎng)址和鏈接文本操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Python-for循環(huán)的內(nèi)部機(jī)制
這篇文章主要介紹了Python for循環(huán)的內(nèi)部機(jī)制,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

