还不知道送女朋友什么颜色口红吗?京东口红数据爬取并可视化帮你分析!

第一步,导入模块

import re
import requests
import time
import matplotlib.pyplot as plt

第二步,获取请求头,并设置反爬。反爬只需要在请求头中添加Referer就OK!

headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0','Referer':'https://item.jd.com/100006079301.html',}

第三步,遍历评论网页,匹配Json文件中的productColor信息,替换掉匹配字符串的空格,并写入文件。为防止被识别,爬一页休眠一秒。头铁的删掉 :)

for i in range(1,100):
    
    url='https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=100006079301&score=0&sortType=5&page='+str(i)+'&pageSize=10&isShadowSku=0&rid=0&fold=1'
    comment=requests.get(url,headers=headers)
    html=comment.text

    data=re.findall(r'"productColor":"(.*?)","',html)
   
    for i in data:
        str_i=i.replace(" ","")
        print(str_i)
        with open("red.txt",'a') as f:
            f.write(str_i+"\n")
            f.close()
    time.sleep(2)
        
print("All succeed!")

 

第四步,读取文件中的颜色信息,并使用count函数统计各颜色出现的次数,并将数据写入柱状图。

openfile=open("D:\\ProgramFiles\\Python3\\red.txt")
filetxt=openfile.read()

a=filetxt.count("胡萝卜色")
b=filetxt.count("粉金管#196半哑光")
c=filetxt.count("粉金管小野莓#888")
d=filetxt.count("粉金管#274奶茶色")
e=filetxt.count("505#朱砂红")
f=filetxt.count("唇釉#288橘咖小奶猫")
g=filetxt.count("粉金管#888小野莓")
h=filetxt.count("情人节限定196")
i=filetxt.count("粉金管#525樱桃红")
j=filetxt.count("蕾丝版274+粉金管196+粉金管888")


x = ["胡萝卜色", "半哑光", "小野莓888", "奶茶色", "朱砂红", "唇釉橘咖", "情人节限定196", "粉金管#525 樱桃红","蕾丝版888"]
y = [a, b, c, d, e, f, g, h,i]

fig, ax = plt.subplots(figsize=(10, 7))
ax.bar(x=x, height=y)
ax.set_title("口红颜色销售数量", fontsize=10)
plt.show()

OK啦,数据展示出来就是这样子啦。

 

 搞不懂,为啥女生都喜欢胡萝卜色???

好啦,还有什么疑问欢迎评论区交流!

 

 

 

 

 

 

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值