python大数据可视化分析淘宝商品,开专卖店不行啊
现如今大数据分析异常火爆,如何正确分析数据,并且抓住数据特点,获得不为人知的秘密?今天沉默带你用python爬虫,爬取淘宝网站进行淘宝商品大数据分析的实战!文章目录
前言
通过这场项目实战,我将带你进入大数据分析的世界,并且学习爬虫技术,pandas,pyecharts,matplotlib等技术。一、明确爬取方向
淘宝的商品数量是特别巨大的,如此海量的数据我们如何去爬取,并且分析?因此我们需要明确爬取方向。怎么才可以爬取自己想分析的数据?我们最有效的方法就是从淘宝主页的搜索接口找到突破口。1.1 淘宝搜索接口的分析
淘宝web网站
我总结如下步骤:
- 第一步:登录淘宝网站,获取我们登录的淘宝账号
- 第二步:获取我们的cookie
- 第三步:获取搜索接口
- 第四步:分析接口,确定爬取数量
1.1.1 cookie获取的途径
我们登录上淘宝账户后,按电脑的F12键,进入开发者模式
点击Network,这里就是淘宝前端接口交互的信息,然后我们随便在搜索框中搜索信息,然后在开发者框中找search接口,点击该接口,你就会找到cookie信息,复制一份信息,保存起来,待后续爬虫使用。
1.1.2 搜索接口的分析
分析搜索接口
https://s.taobao.com/search?q=小米手机&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20200415&ie=utf8&sort=sale-desc&bcoffset=0&p4ppushleft=%2C44&s=0
https://s.taobao.com/search?q=小米手机&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20200415&ie=utf8&sort=sale-desc&bcoffset=0&p4ppushleft=%2C44&s=44
https://s.taobao.com/search?q=小米手机&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20200415&ie=utf8&sort=sale-desc&bcoffset=0&p4ppushleft=%2C44&s=88
分析得知:每个页面参请求数基本相同,只有最后一个页码参数不同,而且是规律的:当前页面数据(页数-1)*
所以当我们想要将前端页面跳转至下一页爬取数据只需要将url拼接上面逻辑
运算后的数据即可。
二、爬虫脚本编写
以男士衬衫商品为例子,开展的简单数据分析,其目的是了解淘宝网站线上销售男士内裤的方法和模式。通过获取到的淘宝网站男士衬衫销售数据情况,进一步分析和判断出哪个价格区间及品牌等信息更加受到网购消费者的青睐和偏好,从而给自己买一个性价比较好的衬衫。1.1引入库
代码如下(局部):
# -*- coding: utf-8 -*-
import requests
import re
import pandas as pd
import time
如果上述的的库没有下载,请安装后在使用;(如果没有安装库,就会爆红)
## 1.2 获取页面信息
# 此处写入登录之后自己的cookies
cookie = ''
# 获取页面信息
def getHTMLText(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'}
user_cookies = cookie
cookies = {
}
for a in user_cookies.split(';'): # 因为cookies是字典形式,所以用spilt函数将之改为字典形式
name, value = a.strip().split('=', 1)
cookies[name] = value
try:
r = requests.get(url, cookies=cookies, headers=headers, timeout=60)
print(r.status_code)
print(r.cookies)
return r.text
except:
print('获取页面信息失败')
return ''
1.3 格式化页面,查找数据
# 格式化页面,查找数据
def parsePage(html):
list = []
try:
views_title = re.findall('"raw_title":"(.*?)","pic_url"', html)
print(len(views_title)) # 打印检索到数据信息的个数,如果此个数与后面的不一致,则数据信息不能加入列表
print(views_title)
views_price = re.findall('"view_price":"(.*?)","view_fee"', html)
print(len(views_price))
print(views_price)
item_loc = re.findall('"item_loc":"(.*?)","view_sales"', html)
print(len(item_loc))
print(item_loc)
views_sales = re.findall('"view_sales":"(.*?)","comment_count"', html)
print(len(views_sales))
print(views_sales)
comment_count = re.findall('"comment_count":"(.*?)","user_id"', html)
print(len(comment_count))
print(comment_count)
shop_name = re.findall('"nick":"(.*?)","shopcard"', html)
print(len(shop_name))
for i in range(len(views_price)):
list.append([views_title[i], views_price[i], item_loc[i], comment_count[i], views_sales[i], shop_name[i]])
# print(list)
print('爬取数据成功')
return list
except:
print('有数据信息不全,如某一页面中某一商品缺少地区信息')
1.4 将数据存储到csv文件中
# 存储到csv文件中,为接下来的数据分析做准备
def save_to_file(list):
data = pd.DataFrame(list)
data.to_csv