前言💨

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

前文内容💨

Python爬虫入门教程01:豆瓣Top电影爬取

Python爬虫入门教程02:小说爬取

Python爬虫入门教程03:二手房数据爬取

Python爬虫入门教程04:招聘信息爬取

Python爬虫入门教程05:B站视频弹幕的爬取

Python爬虫入门教程06:爬取数据后的词云图制作

Python爬虫入门教程07:腾讯视频弹幕爬取

Python爬虫入门教程08:爬取csdn文章保存成PDF

Python爬虫入门教程09:多线程爬取表情包图片

Python爬虫入门教程10:彼岸壁纸爬取

Python爬虫入门教程11:新版王者荣耀皮肤图片的爬取

Python爬虫入门教程12:英雄联盟皮肤图片的爬取

Python爬虫入门教程13:高质量电脑桌面壁纸爬取

Python爬虫入门教程14:有声书音频爬取

Python爬虫入门教程15:音乐网站数据的爬取

Python爬虫入门教程17:音乐歌曲的爬取

Python爬虫入门教程18:好看视频的爬取

Python爬取入门教程19:YY短视频的爬取

Python爬虫入门教程20:IP代理的爬取使用

Python爬虫入门教程21:付费文档的爬取

Python爬虫入门教程22:百度翻译JS解密

Python爬虫入门教程23:A站视频的爬取,解密m3u8视频格式

Python爬虫入门教程24:下载某网站付费文档保存PDF

Python爬虫入门教程25:绕过JS加密参数,实现批量下载抖某音无水印视频内容

Python爬虫入门教程26:快手视频网站数据内容下载

PS:如有需要 Python学习资料 以及 解答 的小伙伴可以加点击下方链接自行获取
python免费学习资料以及群交流解答点击即可加入

基本开发环境💨

  • Python 3.6
  • Pycharm

相关模块的使用💨

import requests
import parsel
import csv
import pandas as pd
from pyecharts.charts import *
from pyecharts.globals import ThemeType#设定主题
from pyecharts.commons.utils import JsCode
import pyecharts.options as opts

安装Python并添加到环境变量,pip安装需要的相关模块即可。

💥需求数据来源分析

在这里插入图片描述
商品数据内容在网页源代码里面有, 所以直接情况网站数据, 然后通过相应的解析方法提取相关数据内容即可。

💥代码实现

import requests
import parsel
import csv

f = open('书籍信息3.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['标题', '推荐', '评价',
                                           '作者', '出版日期', '出版社',
                                           '原价', '售价', '电子书价格',
                                           '详情页'])

csv_writer.writeheader()

for page in range(1, 26):
    print(f'========================正在保存第{page}页数据内容===================================')
    url = f'http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-year-2017-0-1-{page}'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)

    # print(response.text)
    selector = parsel.Selector(response.text)
    lis = selector.css('.bang_list_mode li')
    for li in lis:
        title = li.css('.name a::attr(title)').get()  # 标题/书名
        recommend = li.css('.tuijian::text').get()  # 推荐
        star = li.css('.star a::text').get()  # 评价
        author = li.css('div:nth-child(5) a:nth-child(1)::attr(title)').get()  # 作者
        date = li.css('div:nth-child(6) span::text').get()  # 出版日期
        press = li.css('div:nth-child(6) a::text').get()  # 出版社
        price_r = li.css('.price .price_r::text').get()  # 原价
        price_n = li.css('.price .price_n::text').get()  # 售价
        price_e = li.css('.price_e span::text').get()  # 电子书价格
        href = li.css('.name a::attr(href)').get()  # 详情页
        dit = {
            '标题': title,
            '推荐': recommend,
            '评价': star,
            '作者': author,
            '出版日期': date,
            '出版社': press,
            '原价': price_r,
            '售价': price_n,
            '电子书价格': price_e,
            '详情页': href,
        }
        # with open('书籍.csv', mode='a', encoding='utf-8') as f:
        #     f.write(f'{title},{recommend},{star},{author},{date},{press},{price_r},{price_n},{price_e}\n')
        csv_writer.writerow(dit)
        print(title, recommend, star, author, date, press, price_r, price_n, price_e, sep=' | ')

💥实现效果

在这里插入图片描述
在这里插入图片描述

💥数据可视化效果

❤书籍原价价格区间❤

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="{b}:{d}%"))
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="当当网书籍\n\n原价价格区间", 
            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="{b}:{d}%"))
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="当当网书籍\n\n售价价格区间", 
            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()

在这里插入图片描述
❤各个出版社书籍数量柱状图❤

bar=(
    Bar(init_opts=opts.InitOpts(height='500px',width='1000px',theme='dark'))
    .add_xaxis(counts.index.tolist())
    .add_yaxis(
        '出版社书籍数量',
        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='各个出版社书籍数量柱状图'),
            xaxis_opts=opts.AxisOpts(name='书籍名称',
            type_='category',                                           
            axislabel_opts=opts.LabelOpts(rotate=90),
        ),
        yaxis_opts=opts.AxisOpts(
            name='数量',
            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()

在这里插入图片描述
❤电子书版本占比❤

c = (
    Liquid()
    .add("lq", [1-per], is_outline_show=False)
    .set_global_opts(title_opts=opts.TitleOpts(title="电子书版本占比"))
)
c.render_notebook()

在这里插入图片描述
❤书籍评论数最高Top20❤

bar=(
    Bar(init_opts=opts.InitOpts(height='500px',width='1000px',theme='dark'))
    .add_xaxis(price_top.index.tolist())
    .add_yaxis(
        '书籍单价',
        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='单价最高的书籍详细柱状图'),
            xaxis_opts=opts.AxisOpts(name='书籍名称',
            type_='category',                                           
            axislabel_opts=opts.LabelOpts(rotate=90),
        ),
        yaxis_opts=opts.AxisOpts(
            name='单价/元',
            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()

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐