当今时代智能化发展迅速,各种电子产品层出不穷,人们逐渐从书店购买到网上购买,网上购物使人们更加方便,可以足不出户的购买自己想要的物品,利用爬虫爬取当当网上图书销量排名,然后使用可视化分析,分析人们偏爱的图书价位、风格、出版社等。
二、大数据分析方案:
三、数据分析的实现步骤:
(一)数据爬取
2、发送请求,使用python代码headers请求
3、解析数据,提取我们想要的数据内容
selector=parsel.Selector(response.text)
lis=selector.css('ul.bang_listli')
#Updated:Definecsv_writerbeforetheloopfile_path=r'D:\新建文件夹\期末项目\当当图书.csv'withopen(file_path,mode='a',encoding='utf-8',newline='')asf:csv_writer=csv.DictWriter(f,fieldnames=fieldnames)csv_writer.writeheader()
4、多页爬取
5、保存数据,并存储为.csv文件
#Updatedfilepathfile_path=r'D:\新建文件夹\期末项目\当当图书.csv'withopen(file_path,mode='a',encoding='utf-8',newline='')asf:csv_writer=csv.DictWriter(f,fieldnames=dit.keys())csv_writer.writeheader()
运行结果:
(二)数据可视化
1、引入模块
importpandasaspdfrompyecharts.chartsimport*frompyecharts.globalsimportThemeType#设定主题frompyecharts.commons.utilsimportJsCodeimportpyecharts.optionsasopts
2、导入数据
df=pd.read_csv('当当图书.csv',encoding='utf-8',engine='python')df.head()
结果:
3、数据可视化
frompyecharts.chartsimportPiefrompyechartsimportoptionsasopts
#Sampledatafordemonstrationdatas_pair_1=[("0-30",20),("30-70",60),("70-150",10),("150-300",10)]
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(
4、分析价格最高的图书
importpandasaspd
#Sortthedatabypriceindescendingorderdata=data.sort_values(by='单价',ascending=False)
完整代码:
importrequestsimportparselimportcsvimporttime
#数据导入
importpandasaspdfrompyecharts.chartsimport*frompyecharts.globalsimportThemeType#设定主题frompyecharts.commons.utilsimportJsCodeimportpyecharts.optionsasoptsdf=pd.read_csv('当当图书.csv',encoding='utf-8',engine='python')df.head()
#数据可视化
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()
#分析单价最高
frompyecharts.chartsimportBarfrompyechartsimportoptionsasoptsfrompyecharts.commons.utilsimportJsCode
counts=dict(zip(categories,values))
四、总结
对本课程设计的整体完成情况做一个总结,通过这次利用python代码爬虫爬取网站数据并分析,让我掌握了最基础的数据分析知识,体验了数据分析的乐趣,包括数据预处理,数据清洗,异常值的查找等,数据的合并和分组及聚合,还有数据可视化来直观的观察.分析数据。虽然学的不是很精通,部分代码还是需要借鉴下课本,网络。但在这次课程设计中还是学到了很多,不止是进一步的复习了课本上的知识内容,还学习了他人的代码思路并加以思考形成自己的思路。虽然作品仍有残缺不是很完美,但希望可以越来越进步