前言
抖音帶貨的興起,讓抖音電商一躍成為與淘寶電商、京東電商等電商平臺共同爭奪電商市場的存在,與淘寶電商、京東電商等電商平臺相比,抖音電商擁有獨特的優(yōu)勢,抖音以短視頻的形式能夠帶來巨大的流量和熱度,抖音以此為基礎(chǔ)帶來全新的帶貨方式——短視頻帶貨,除此之外,抖音因其高流量高熱度的基礎(chǔ),使得開展直播帶貨的途徑順其自然了許多。在此背景之下,抖音電商的商品銷售數(shù)據(jù)的獲取和總結(jié)分析歸納對于抖音電商調(diào)整當(dāng)前銷售結(jié)構(gòu)和形式以及對未來的規(guī)劃有著重大意義。
除此之外,也給一些想要簡單爬取一些抖音商品銷售數(shù)據(jù)的同學(xué)們一個便捷易懂的方法。
目標(biāo)
由于通過抖音頁面對商品銷售數(shù)據(jù)進行爬取過于繁瑣,工作量大,因此決定通過對蟬媽媽數(shù)據(jù)平臺(擁有抖音商品銷售數(shù)據(jù)的各大榜單)進行抖音商品銷售數(shù)據(jù)的獲取,又因想要通過蟬媽媽數(shù)據(jù)平臺直接進行數(shù)據(jù)的獲取所需要的成本和花銷太大,因此決定通過以爬蟲的形式對蟬媽媽數(shù)據(jù)平臺的各大抖音商品銷售數(shù)據(jù)榜單進行數(shù)據(jù)爬取,由新人注冊蟬媽媽數(shù)據(jù)平臺都會獲得免費的會員時間,但免費的會員只能夠查看數(shù)據(jù),并不能對數(shù)據(jù)的直接導(dǎo)出,因此可以通過免費的會員使用爬蟲對各大榜單數(shù)據(jù)進行爬取。
打開蟬媽媽數(shù)據(jù)平臺以后,選擇一個銷售榜單,本文以直播帶貨的榜單為例,后續(xù)的網(wǎng)頁下載、數(shù)據(jù)解析、數(shù)據(jù)存儲和數(shù)據(jù)預(yù)處理都以直播帶貨榜單開展,其他榜單的相關(guān)處理與直播帶貨榜單相差不大,所以就以直播帶貨榜單為例,爬取方法以selenium自動化庫為主。
爬取過程
網(wǎng)頁源代碼查看及分析
查看網(wǎng)頁源代碼以后,簡單知道每一條直播帶貨的數(shù)據(jù)都存儲在<tr></tr>
標(biāo)簽中,因此我們便可得知,只要抓取所有的<tr></tr>
標(biāo)簽,再對所有的<tr></tr>
標(biāo)簽進行處理,對每一個<tr></tr>
標(biāo)簽里的數(shù)據(jù)進行分割讀?。恳粋€<td></td>
標(biāo)簽內(nèi)含有的便是每一條直播帶貨數(shù)據(jù):直播銷量、直播銷售額等),就能得到每一條直播帶貨數(shù)據(jù)。
上圖可知一條直播銷售具體數(shù)據(jù)存儲在<tr></tr>
標(biāo)簽中的每一個<td></td>
標(biāo)簽中。
因此只要獲取每個<tr></tr>
標(biāo)簽,再從每個標(biāo)簽中逐一分析<td></td>
所含具體商品銷售數(shù)據(jù),即可獲得預(yù)計要得到的內(nèi)容。
網(wǎng)頁下載
由于為了應(yīng)對反爬機制,本爬蟲主要使用了selenium自動化模塊,因此網(wǎng)頁下載部分沒有一個單獨的模塊用來進行網(wǎng)頁下載,因此也就沒有ip代理池、headers、cookies的設(shè)置,所有就只有一行代碼進行網(wǎng)頁的下載(selenium.webdriver中自帶的網(wǎng)頁下載方法)
html = driver.page_source
數(shù)據(jù)解析
根據(jù)上述的網(wǎng)頁源代碼分析,對每一個<td></td>
標(biāo)簽進行解析,對要得到的每一個商品特征信息進行查看,對其html位置進行解析,便可得到以下代碼。
數(shù)據(jù)解析單獨建立一個模塊。解析過程中使用bs解析器。
from bs4 import BeautifulSoup
from selenium import webdriver
class WatchingParser:
def __init__(self):
self.base_url = "https://www.chanmama.com/"
self.driver = webdriver.Chrome()
def get_data(self,html_content):
print("開始解析數(shù)據(jù)")
if html_content is None:
return None
# 匯總當(dāng)前頁所有數(shù)據(jù)
result_data = []
soup = BeautifulSoup(html_content,'lxml')
# 獲取所有tr標(biāo)簽
all_data = soup.find_all('tr')
all_data = all_data[2:]
i = 1
# 分析每一個tr標(biāo)簽的數(shù)據(jù)
try:
while i < 51:
data = all_data[i]
# 獲取直播名稱
name = data.find_all('a',class_='text-decoration-none c333 link-hover cursor-pointer')
watch_name = name[0].text
watch_name = watch_name.replace("\n", "")
watch_name = watch_name.replace(" ", "")
# print(watch_name)
# 獲取達人名稱
people_name = name[1].text
people_name = people_name.replace("\n", "")
people_name = people_name.replace(" ", "")
# print(people_name)
# 獲取達人粉絲數(shù)
fans_quantity = data.find('span',class_='c999').text
fans_quantity = fans_quantity.replace("\n", "")
fans_quantity = fans_quantity.replace(" ", "")
# print(fans_quantity)
# 獲取開播時間
watch_start = data.find('td',class_='fs14').text
watch_start = watch_start.replace("\n", "")
watch_start = watch_start.replace(" ", "")
# print(watch_start)
# 獲取直播時長
watch_all = data.find_all('div',class_='text-align-left pl15')
watch_longth = watch_all[1].text
watch_longth = watch_longth.replace("\n", "")
watch_longth = watch_longth.replace(" ", "")
# print(watch_longth)
# 獲取人氣峰值
peak = data.find('td',class_='text-align-right').text
peak = peak.replace("\n", "")
peak = peak.replace(" ", "")
# print(peak)
# 獲取觀看人次
watching_people = data.find('td',class_='divider text-align-right').text
watching_people = watching_people.replace("\n", "")
watching_people = watching_people.replace(" ", "")
# print(watching_people)
commodity = data.find_all('div',class_='text-align-right pr20')
# 獲取商品數(shù)
commodity_quantity = commodity[0].text
commodity_quantity = commodity_quantity.replace("\n", "")
commodity_quantity = commodity_quantity.replace(" ", "")
# print(commodity_quantity)
# 獲取銷售額
sale_money = commodity[1].text
sale_money = sale_money.replace("\n", "")
sale_money = sale_money.replace(" ", "")
# print(sale_money)
# 獲取銷售量
sale_quantity = commodity[2].text
sale_quantity = sale_quantity.replace("\n", "")
sale_quantity = sale_quantity.replace(" ", "")
# print(sale_quantity)
item = {
'wacth_name' : watch_name,
'people_name' : people_name,
'fans_quantity' : fans_quantity,
'watch_start' : watch_start,
'watch_longth' : watch_longth,
'peak' : peak,
'wacthing_people' : watching_people,
'commodity_quantity' : commodity_quantity,
'sale_money' : sale_money,
'sale_quantity' : sale_quantity
}
# print(item)
result_data.append(item)
i = i + 1
# print(i)
except Exception as e:
print(e)
return result_data
數(shù)據(jù)存儲
數(shù)據(jù)存儲部分也單獨建立一個模塊。
import csv
import pymongo
class DataStorage:
def __init__(self):
self.alldata=[]
def store_data(self, data):
if data is None:
return
self.alldata.append(data)
def watching_output_file(self,filename):
print("開始將數(shù)據(jù)寫入文件")
file = open(filename, 'w', newline='', encoding='utf-8-sig')
csv_writer = csv.writer(file, dialect='excel')
#由于數(shù)據(jù)存儲的是二維數(shù)組([{,},{},{},{}] 形式的,因此需要先讀取一個{},再對里面的信息進行鍵值對分離
for data in self.alldata:
print(data)
for note in data:
print(note)
cols = note.items()
row = []
for key, value in cols:
row.append(value)
csv_writer.writerow(row)
file.close()
主程序
主程序運行,將數(shù)據(jù)解析和數(shù)據(jù)存儲模塊導(dǎo)入。
import time
from WatchingParser import *
from DataStorage import *
from selenium import webdriver
# 達人直播帶貨榜單數(shù)據(jù)獲取
class WacthingMain:
def __init__(self):
self.parser = WatchingParser()
self.datastorage = DataStorage()
def set_limit(self, limit):
self.urlmanager.set_limit(limit)
def crawl(self, root_url, filename):
driver = webdriver.Chrome(executable_path="D:\\Spider\\tiktok\\chromedriver\\chromedriver.exe")
# 模擬登錄,訪問直播帶貨頁面
driver.get(root_url)
time.sleep(7)
# 進行賬號的模擬登錄
driver.find_element_by_id('e2e-login-btn').click()
time.sleep(3)
driver.find_element_by_id('e2e-login-username').send_keys('17858855600')
driver.find_element_by_id('e2e-login-password').send_keys('123456')
driver.find_element_by_id('e2e-login-submit').click()
time.sleep(5)
# 進行30頁的數(shù)據(jù)爬取
i = 1
while i < 21:
print("進行第" + str(i) + "頁數(shù)據(jù)抓取")
try:
time.sleep(7)
html = driver.page_source
# 對當(dāng)前網(wǎng)頁的html進行解析,獲取當(dāng)前商品信息,存入data
data = self.parser.get_data(html)
print(data)
# 將data存入到datastorage的all_data屬性中
if data:
self.datastorage.store_data(data)
print("成功抓取到第" + str(i) + "頁數(shù)據(jù)")
except Exception as e:
print(e)
print("第" + str(i) + "頁抓取失敗")
#下一頁點擊
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div/div/div/div/div[2]/div[4]/div/button[2]/i").click()
i = i + 1
driver.quit()
self.datastorage.watching_output_file(filename)
if __name__ == '__main__':
root_url="https://www.chanmama.com/liveSearch?keyword=&search_type=0&follower_count=&gift_count=&volume=&is_take_product=0"
spider_main = WacthingMain()
spider_main.crawl(root_url,'D:\\Spider\\tiktok\\wacthing_data.csv')
數(shù)據(jù)預(yù)處理
對爬取到的數(shù)據(jù)進行一些簡單的數(shù)據(jù)預(yù)處理,以下為爬取到的數(shù)據(jù)。
使用excel中的分列功能,對其進行以,為分隔符進行分割,并且使用刪除重復(fù)值,得到以下的數(shù)據(jù)表。
由于粉絲數(shù)等具有w字單位的,會對后續(xù)的分析有影響,所有統(tǒng)一將w替換成000,將其變成數(shù)值類型,方便后續(xù)的分析。
由于銷售量和銷售額是一個區(qū)間,再根據(jù)其是一個固定的劃分區(qū)間,具有一個遞增的規(guī)律,因此對每個區(qū)間進行等級賦值,得到銷售量等級數(shù)據(jù)列,方便后續(xù)的分析。得到以下的數(shù)據(jù)表。文章來源:http://www.zghlxwxcb.cn/news/detail-712751.html
總結(jié)和不足
總體代碼運用了selenium自動化模塊,能夠有效地應(yīng)對平臺的反爬機制,從而有效地獲取到網(wǎng)頁的數(shù)據(jù),但又由于使用的是selenium自動化模塊,所以總體的代碼運行效率不是很高,耗時長。
整個數(shù)據(jù)預(yù)處理總體上使用了excel的功能,雖有效地解決了要解決的問題,但有些處理過程并不簡單有效率,預(yù)處理只做了剔除重復(fù)值,刪除空白字段,和對銷售區(qū)間進行等級映射賦值,還有其他的預(yù)處理過程并未進行,使得數(shù)據(jù)格式或有效程度未達到最大。文章來源地址http://www.zghlxwxcb.cn/news/detail-712751.html
到了這里,關(guān)于爬蟲——有關(guān)抖音商品數(shù)據(jù)的爬?。ㄏs媽媽數(shù)據(jù)平臺)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!