Python爬取當(dāng)網(wǎng)書籍?dāng)?shù)據(jù)并數(shù)據(jù)可視化展示
一、開發(fā)環(huán)境
- Python 3.8
- Pycharm 2021.2 專業(yè)版
二、模塊使用
csv
模塊 把爬取下來的數(shù)據(jù)保存表格里面的 內(nèi)置模塊requests >>> pip install requests
數(shù)據(jù)請(qǐng)求模塊parsel >>> pip install parsel
數(shù)據(jù)解析模塊 css選擇器去提取數(shù)據(jù)
三、爬蟲代碼實(shí)現(xiàn)步驟
- 導(dǎo)入所需模塊
- 發(fā)送請(qǐng)求, 用
python
代碼模擬瀏覽器發(fā)送請(qǐng)求 - 解析數(shù)據(jù), 提取我們想要數(shù)據(jù)內(nèi)容
- 多頁爬取
- 保存數(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
用戶代理 瀏覽器的基本身份標(biāo)識(shí)
標(biāo)題中無效的返回字符或前導(dǎo)空格:User-Agent
不要留有空格
通過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ù)類型 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類名name標(biāo)簽 a 標(biāo)簽 attr() 屬性選擇器 取a標(biāo)簽里面title屬性 get() 獲取數(shù)據(jù) ? ? title = li.css('.name a::attr(title)').get() ?# 書名 ? ? # 獲取標(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() ?# 電子書價(jià)格 ? ? href = li.css('.name a::attr(href)').get() ?# 詳情頁 ? ? dit = { ? ? ? ? '書名': title, ? ? ? ? '評(píng)論數(shù)': comment, ? ? ? ? '推薦量': recommend, ? ? ? ? '作者': author, ? ? ? ? '出版社': publish, ? ? ? ? '售價(jià)': price_n, ? ? ? ? '原價(jià)': price_r, ? ? ? ? '折扣': price_s, ? ? ? ? '電子書價(jià)格': price_e, ? ? ? ? '詳情頁': 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. 多頁爬取
for page in range(1, 26): ? ? # 字符串格式化方法 ? ? print(f'正在爬取第{page}頁的數(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)建打開一個(gè)文件 進(jìn)行保存 f = open('當(dāng)當(dāng)圖書.csv', mode='a', encoding='utf-8', newline='') csv_writer = csv.DictWriter(f, fieldnames=[ ? ? '書名', ? ? '評(píng)論數(shù)', ? ? '推薦量', ? ? '作者', ? ? '出版社', ? ? '售價(jià)', ? ? '原價(jià)', ? ? '折扣', ? ? '電子書價(jià)格', ? ? '詳情頁', ]) csv_writer.writeheader() ?# 寫入表頭
運(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('書籍信息.csv', encoding='utf-8', engine='python') df.head()
3.可視化
書籍總體價(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=":ublnpf9mb%")) ? ? .set_global_opts( ? ? ? ? title_opts=opts.TitleOpts( ? ? ? ? ? ? title="當(dāng)當(dāng)網(wǎng)書籍\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=":ublnpf9mb%")) ? ? .set_global_opts( ? ? ? ? title_opts=opts.TitleOpts( ? ? ? ? ? ? title="當(dāng)當(dāng)網(wǎng)書籍\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è)出版社書籍?dāng)?shù)量柱狀圖:
bar=( ? ? Bar(init_opts=opts.InitOpts(height='500px',width='1000px',theme='dark')) ? ? .add_xaxis(counts.index.tolist()) ? ? .add_yaxis( ? ? ? ? '出版社書籍?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è)出版社書籍?dāng)?shù)量柱狀圖'), ? ? ? ? ? ? xaxis_opts=opts.AxisOpts(name='書籍名稱', ? ? ? ? ? ? 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()
書籍評(píng)論數(shù)最高Top20:
bar=( ? ? Bar(init_opts=opts.InitOpts(height='500px',width='1000px',theme='dark')) ? ? .add_xaxis(price_top.index.tolist()) ? ? .add_yaxis( ? ? ? ? '書籍單價(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à)最高的書籍詳細(xì)柱狀圖'), ? ? ? ? ? ? xaxis_opts=opts.AxisOpts(name='書籍名稱', ? ? ? ? ? ? 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)書籍?dāng)?shù)據(jù)并數(shù)據(jù)可視化展示的文章就介紹到這了,更多相關(guān)Python爬取當(dāng)網(wǎng)書籍?dāng)?shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中列表的切片與修改知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享了關(guān)于python中列表的切片與修改的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-07-07Python中順序表原理與實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Python中順序表原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Python順序表的概念、原理及增刪查等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-12-12Selenium+Python自動(dòng)化測(cè)試入門
本文主要介紹了Selenium+Python自動(dòng)化測(cè)試入門,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08利于python腳本編寫可視化nmap和masscan的方法
這篇文章主要介紹了利于python腳本編寫可視化nmap和masscan的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12淺談Pytorch中autograd的若干(踩坑)總結(jié)
這篇文章主要介紹了Pytorch中autograd的若干(踩坑)總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-05-05python實(shí)現(xiàn)水仙花數(shù)實(shí)例講解
這篇文章主要介紹了python實(shí)現(xiàn)水仙花數(shù)實(shí)例講解,有正在學(xué)習(xí)python的同學(xué)可以跟著小編一起來學(xué)習(xí)下水仙花數(shù)怎么用python計(jì)算吧2021-03-03教你如何使用Python開發(fā)一個(gè)釘釘群應(yīng)答機(jī)器人
在聊天工具大肆侵入我們生活各個(gè)方面的今天,各種消息無時(shí)無刻不在侵?jǐn)_我們的每一寸時(shí)間,這種情況下,一個(gè)聊天的機(jī)器人就很有必要了.今天,我們來學(xué)習(xí)一下使用 Python 開發(fā)一個(gè)釘釘?shù)膽?yīng)答機(jī)器人,助你「人生苦短,少回消息」,需要的朋友可以參考下2021-06-06