自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

手把手教你采集京東銷售數(shù)據(jù)并做簡單的數(shù)據(jù)分析和可視化

大數(shù)據(jù) 數(shù)據(jù)可視化
隨著移動(dòng)支付的普及,電商網(wǎng)站不斷涌現(xiàn),由于電商網(wǎng)站產(chǎn)品太多,由用戶產(chǎn)生的評(píng)論數(shù)據(jù)就更多了,這次我們以京東為例,針對(duì)某一單品的評(píng)論數(shù)據(jù)進(jìn)行數(shù)據(jù)采集,并且做簡單數(shù)據(jù)分析。

[[421418]]

前言

大家好!我是古月星辰,大三本科生,數(shù)學(xué)專業(yè),Python爬蟲愛好者一枚。今天給大家?guī)鞪D數(shù)據(jù)的簡單采集和可視化分析,希望大家可以喜歡。

一、目標(biāo)數(shù)據(jù)

隨著移動(dòng)支付的普及,電商網(wǎng)站不斷涌現(xiàn),由于電商網(wǎng)站產(chǎn)品太多,由用戶產(chǎn)生的評(píng)論數(shù)據(jù)就更多了,這次我們以京東為例,針對(duì)某一單品的評(píng)論數(shù)據(jù)進(jìn)行數(shù)據(jù)采集,并且做簡單數(shù)據(jù)分析。

二、頁面分析

這個(gè)是某一手機(jī)頁面的詳情頁,對(duì)應(yīng)著手機(jī)的各種參數(shù)以及用戶評(píng)論信息,頁面URL是:

  1. https://item.jd.com/10022971060622.html#none 

然后通過分析找到評(píng)論數(shù)據(jù)對(duì)應(yīng)的數(shù)據(jù)接口,如下圖所示:

它的請(qǐng)求url:

  1. https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_com 
  2. ment98& productId=10022971060622 &score=0&sortType=5& page=0 &pageSize=10&isShadowSk 
  3. u=0&fold=1 

注意看到這兩個(gè)關(guān)鍵參數(shù)

1. productId: 每個(gè)商品有一個(gè)id

2. page: 對(duì)應(yīng)的評(píng)論分頁

三、解析數(shù)據(jù)

對(duì)評(píng)論數(shù)據(jù)的url發(fā)起請(qǐng)求:

  1. url:https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comm 
  2. ent98& productId=10022971060622 &score=0&sortType=5& page=0 &pageSize=10&isShado 
  3. wSku=0&fold=1 

json.cn 打開json數(shù)據(jù)(我們的評(píng)論數(shù)據(jù)是以json形式與頁面進(jìn)行交互傳輸?shù)?,如下圖所示:

分析可知,評(píng)論url中對(duì)應(yīng)十條評(píng)論數(shù)據(jù),對(duì)于每一條評(píng)論數(shù)據(jù),我們需要獲取3條數(shù)

據(jù),contents,color,size(注意到上圖的maxsize,100,也就是100*10=1000條評(píng)論)。

四、程序

1.導(dǎo)入相關(guān)庫

  1. import  requests 
  2. import  json 
  3. import  time 
  4. import  openpyxl  #第三方模塊,用于操作Excel文件的 
  5. #模擬瀏覽器發(fā)送請(qǐng)求并獲取響應(yīng)結(jié)果 
  6. import random 

2.獲取評(píng)論數(shù)據(jù)

  1. def get_comments(productId,page): 
  2.     url='https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId={0}&score=0&sortType=5&page={1}&pageSize=10&isShadowSku=0&fold=1'.format(productId,page) # 商品id 
  3.     resp=requests.get(url,headers=headers) 
  4.     #print(resp.text)  #響應(yīng)結(jié)果進(jìn)行顯示輸出 
  5.     s1=resp.text.replace('fetchJSON_comment98(','') #fetchJSON_comment98( 
  6.     s=s1.replace(');',''
  7.     #將str類型的數(shù)據(jù)轉(zhuǎn)成json格式的數(shù)據(jù) 
  8.     # print(s,type(s)) 
  9.     # print('*'*100) 
  10.     res=json.loads(s) 
  11.     print(type(res)) 
  12.     return res 

3.獲取最大頁數(shù)(也可以不寫)

  1. def get_max_page(productId): 
  2.     dic_data=get_comments(productId,0)  #調(diào)用剛才寫的函數(shù),向服務(wù)器發(fā)送請(qǐng)求,獲取字典數(shù)據(jù) 
  3.     return dic_data['maxPage'

4.提取數(shù)據(jù)

  1. def get_info(productId): 
  2.     #調(diào)用函數(shù)獲取商品的最大評(píng)論頁數(shù) 
  3.     #max_page=get_max_page(productId) 
  4.     # max_page=10 
  5.     lst=[]  #用于存儲(chǔ)提取到的商品數(shù)據(jù) 
  6.     for page in range(0,get_max_page(productId)):   #循環(huán)執(zhí)行次數(shù) 
  7.         #獲取每頁的商品評(píng)論 
  8.         comments=get_comments(productId,page) 
  9.         comm_lst=comments['comments']   #根據(jù)key獲取value,根據(jù)comments獲取到評(píng)論的列表(每頁有10條評(píng)論) 
  10.         #遍歷評(píng)論列表,分別獲取每條評(píng)論的中的內(nèi)容,顏色,鞋碼 
  11.         for item in comm_lst:   #每條評(píng)論又分別是一個(gè)字典,再繼續(xù)根據(jù)key獲取值 
  12.             content=item['content']  #獲取評(píng)論中的內(nèi)容 
  13.             color=item['productColor'] #獲取評(píng)論中的顏色 
  14.             size=item['productSize'] #鞋碼 
  15.             lst.append([content,color,size])  #將每條評(píng)論的信息添加到列表中 
  16.         time.sleep(3)  #延遲時(shí)間,防止程序執(zhí)行速度太快,被封IP 
  17.     save(lst)  #調(diào)用自己編寫的函數(shù),將列表中的數(shù)據(jù)進(jìn)行存儲(chǔ) 

5.用于將爬取到的數(shù)據(jù)存儲(chǔ)到Excel中

  1. def save(lst): 
  2.     wk=openpyxl.Workbook () #創(chuàng)建工作薄對(duì)象 
  3.     sheet=wk.active  #獲取活動(dòng)表 
  4.     #遍歷列表,將列表中的數(shù)據(jù)添加到工作表中,列表中的一條數(shù)據(jù),在Excel中是 一行 
  5.     for item in lst: 
  6.         sheet.append(item) 
  7.     #保存到磁盤上 
  8.     wk.save('銷售數(shù)據(jù).xlsx'

6.運(yùn)行程序

  1. if __name__ == '__main__'
  2.     productId='10029693009906' # 單品id 
  3.     get_info(productId) 

五、簡單數(shù)據(jù)

1.簡單配置

  1. # 導(dǎo)入相關(guān)庫 
  2. import pandas as pd  
  3. import matplotlib.pyplot as plt 
  4. # 這兩行代碼解決 plt 中文顯示的問題 
  5. plt.rcParams['font.sans-serif'] = ['SimHei'
  6. plt.rcParams['axes.unicode_minus'] = False 
  7. # 由于采集的時(shí)候沒有設(shè)置表頭,此處設(shè)置表頭 
  8. data = pd.read_excel('./銷售數(shù)據(jù).xlsx', header=None, names = ['comments','color','intro'] ) #  
  9. data.head() 

2.手機(jī)顏色數(shù)量對(duì)比

  1. x = ['白色','黑色','綠色','藍(lán)色','紅色','紫色'
  2. y = [314,295,181,173,27,10] 
  3. plt.bar(x,y) 
  4. plt.title('各種顏色手機(jī)數(shù)量對(duì)比'
  5. plt.xlabel('顏色'
  6. plt.ylabel('數(shù)量'
  7. # plt.legend() # 顯示圖例 
  8. plt.show() 

可以看出用戶購買的手機(jī)白色和黑色的機(jī)型比較多.占據(jù)了60%多。3.評(píng)論詞云展示1)先要提取評(píng)論數(shù)據(jù)

  1. import xlrd 
  2. def strs(row): 
  3.     values = ""
  4.     for i in range(len(row)): 
  5.         if i == len(row) - 1: 
  6.             values = values + str(row[i]) 
  7.         else
  8.             values = values + str(row[i]) 
  9.     return values 
  10. # 打卡文件 
  11. data = xlrd.open_workbook("./銷售數(shù)據(jù).xlsx"
  12. sqlfile = open("data.txt""a")  # 文件讀寫方式是追加 
  13. table = data.sheets()[0]  # 表頭 
  14. nrows = table.nrows  # 行數(shù) 
  15. ncols = table.ncols  # 列數(shù) 
  16. colnames = table.row_values(1)  # 某一行數(shù)據(jù) 
  17. # 打印出行數(shù)列數(shù) 
  18. for ronum in range(1, nrows): 
  19.         row = table.cell_value(rowx=ronum, colx = 0) #只需要修改你要讀取的列數(shù)-1 
  20.         values = strs(row)  # 調(diào)用函數(shù),將行數(shù)據(jù)拼接成字符串 
  21.         sqlfile.writelines(values + "\n")  # 將字符串寫入新文件 
  22. sqlfile.close()  # 關(guān)閉寫入的文件 

2)詞云展示

  1. # 導(dǎo)入相應(yīng)的庫 
  2. import jieba 
  3. from PIL import Image 
  4. import numpy as np 
  5. from wordcloud import WordCloud 
  6. import matplotlib.pyplot as plt 
  7. # 導(dǎo)入文本數(shù)據(jù)并進(jìn)行簡單的文本處理 
  8. # 去掉換行符和空格 
  9. text = open("./data.txt",encoding='gbk').read() 
  10. text = text.replace('\n',"").replace("\u3000",""
  11.  
  12. # 分詞,返回結(jié)果為詞的列表 
  13. text_cut = jieba.lcut(text) 
  14. # 將分好的詞用某個(gè)符號(hào)分割開連成字符串 
  15. text_cut = ' '.join(text_cut) 

注意: 這里我們不能使用encoding='uth-8',會(huì)報(bào)出一個(gè)錯(cuò)誤:

  1. 'utf-8' codec can't decode byte 0xd3 in position 0: invalid continuation byte 

所以我們需要改成 gbk。

  1. word_list = jieba.cut(text) 
  2. space_word_list = ' '.join(word_list) 
  3. print(space_word_list) 
  4. # 調(diào)用包PIL中的open方法,讀取圖片文件,通過numpy中的array方法生成數(shù)組 
  5. mask_pic = np.array(Image.open("./xin.png")) 
  6. word = WordCloud( 
  7.     font_path='C:/Windows/Fonts/simfang.ttf',  # 設(shè)置字體,本機(jī)的字體 
  8.     mask=mask_pic,  # 設(shè)置背景圖片 
  9.     background_color='white',  # 設(shè)置背景顏色 
  10.     max_font_size=150,  # 設(shè)置字體最大值 
  11.     max_words=2000,  # 設(shè)置最大顯示字?jǐn)?shù) 
  12.     stopwords={'的'}  # 設(shè)置停用詞,停用詞則不在詞云途中表示 
  13.                  ).generate(space_word_list) 
  14. image = word.to_image() 
  15. word.to_file('2.png')  # 保存圖片 
  16. image.show() 

最后得到的效果圖,如下圖所示:

本文轉(zhuǎn)載自微信公眾號(hào)「Python爬蟲與數(shù)據(jù)挖掘」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系Python爬蟲與數(shù)據(jù)挖掘公眾號(hào)。

 

 

責(zé)任編輯:武曉燕 來源: Python爬蟲與數(shù)據(jù)挖掘
相關(guān)推薦

2017-05-18 12:45:35

數(shù)據(jù)分析數(shù)據(jù)理解數(shù)據(jù)

2020-12-17 09:40:01

Matplotlib數(shù)據(jù)可視化命令

2015-10-26 09:24:30

微信公眾號(hào)數(shù)據(jù)分析

2021-08-26 09:00:48

PyechartsPython可視化

2021-05-10 06:48:11

Python騰訊招聘

2021-07-14 09:00:36

Python數(shù)據(jù)Python基礎(chǔ)

2020-06-17 08:35:12

數(shù)據(jù)分析Python代碼

2017-10-18 16:08:15

可視化交叉驗(yàn)證代碼

2020-05-14 10:19:23

Python可視化分析

2023-02-01 10:16:50

Python可視化

2022-02-09 09:03:42

分詞、詞頻統(tǒng)計(jì)可視化

2021-09-30 18:27:38

數(shù)據(jù)倉庫ETL

2020-06-22 13:41:27

數(shù)據(jù)集數(shù)據(jù)清洗數(shù)據(jù)模型

2021-12-29 20:20:25

結(jié)構(gòu)化數(shù)據(jù)Pandas

2018-04-05 22:55:34

數(shù)據(jù)問答系統(tǒng)SQuAD

2021-09-18 14:26:49

Linux Linux 啟動(dòng)流程Linux 系統(tǒng)

2017-01-12 17:28:59

數(shù)據(jù)分析數(shù)據(jù)可視化可視化

2022-07-24 21:43:48

數(shù)據(jù)可視化大數(shù)據(jù)

2020-04-14 10:20:12

MySQL數(shù)據(jù)庫死鎖

2024-10-16 11:40:47

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)