Python提取淘宝电脑商品信息并分析销量最好的十个商家

淘宝作为国内最大的电商平台之一,商品种类繁多,其中电脑商品的销量一直领先。本篇博客将介绍如何使用Python提取淘宝电脑商品信息(包括商家、销量、品牌),并分析出销量最好的十个商家,并以饼图的形式展示。

一、思路

1.通过Selenium模拟浏览器访问淘宝电脑商品页面,获取页面源代码。

2.利用BeautifulSoup解析页面源代码,获取商品信息。

3.将商品信息存储到本地文件中。

4.分析销量最好的十个商家,并以饼图的形式展示。

二、代码实现

1.导入所需库

from selenium import webdriver
from bs4 import BeautifulSoup
import time
import csv
import matplotlib.pyplot as plt

2.设置浏览器驱动(本篇博客使用Chrome浏览器)

driver = webdriver.Chrome(executable_path=r"C:\chromedriver_win32\chromedriver.exe")

3.访问淘宝电脑商品页面并获取页面源代码

url = 'https://s.taobao.com/search?q=%E7%94%B5%E8%84%91&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20220104&ie=utf8'
driver.get(url)
time.sleep(5) # 等待页面加载完成
html = driver.page_source

4.使用BeautifulSoup解析页面源代码,获取商品信息

soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('div', {'class': 'item J_MouserOnverReq  J_ItemPic'})
data = []
for item in items:
    title = item.find('div', {'class': 'title'}).text.strip()
    price = item.find('strong').text.strip()
    sales = item.find('div', {'class': 'deal-cnt'}).text.strip().replace('人付款', '')
    shop = item.find('div', {'class': 'shop'}).text.strip()
    brand = item.find('div', {'class': 'row row-2 title'}).find_all('span')[1].text.strip()
    data.append([title, price, sales, shop, brand])

5.将商品信息存储到本地文件中

with open('computer.csv', 'w', encoding='utf-8', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['商品名称', '价格', '销量', '商家', '品牌'])
    writer.writerows(data)

6.分析销量最好的十个商家,并以饼图的形式展示

shop_sales = {}
for item in data:
    shop = item[3]
    sales = int(item[2])
    if shop in shop_sales:
        shop_sales[shop] += sales
    else:
        shop_sales[shop] = sales

sorted_shop_sales = sorted(shop_sales.items(), key=lambda x: x[1], reverse=True)[:10]

labels = [item[0] for item in sorted_shop_sales]
values = [item[1] for item in sorted_shop_sales]

plt.pie(values, labels=labels, autopct='%1.1f%%')
plt.title('销量最好的十个商家')
plt.show()

三、完整代码

from selenium import webdriver
from bs4 import BeautifulSoup
import time
import csv
import matplotlib.pyplot as plt

driver = webdriver.Chrome(executable_path=r"C:\chromedriver_win32\chromedriver.exe")

url = 'https://s.taobao.com/search?q=%E7%94%B5%E8%84%91&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20220104&ie=utf8'
driver.get(url)
time.sleep(5) # 等待页面加载完成
html = driver.page_source

soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('div', {'class': 'item J_MouserOnverReq  J_ItemPic'})
data = []
for item in items:
    title = item.find('div', {'class': 'title'}).text.strip()
    price = item.find('strong').text.strip()
    sales = item.find('div', {'class': 'deal-cnt'}).text.strip().replace('人付款', '')
    shop = item.find('div', {'class': 'shop'}).text.strip()
    brand = item.find('div', {'class': 'row row-2 title'}).find_all('span')[1].text.strip()
    data.append([title, price, sales, shop, brand])

with open('computer.csv', 'w', encoding='utf-8', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['商品名称', '价格', '销量', '商家', '品牌'])
    writer.writerows(data)

shop_sales = {}
for item in data:
    shop = item[3]
    sales = int(item[2])
    if shop in shop_sales:
        shop_sales[shop] += sales
    else:
        shop_sales[shop] = sales

sorted_shop_sales = sorted(shop_sales.items(), key=lambda x: x[1], reverse=True)[:10]

labels = [item[0] for item in sorted_shop_sales]
values = [item[1] for item in sorted_shop_sales]

plt.pie(values, labels=labels, autopct='%1.1f%%')
plt.title('销量最好的十个商家')
plt.show()


 

四、结果展示

本篇博客的代码结果将会生成一个名为“computer.csv”的本地文件,包含淘宝电脑商品的商品名称、价格、销量、商家和品牌等信息,以及一个销量最好的十个商家的饼图。

五、总结

本篇博客介绍了如何使用Python提取淘宝电脑商品信息,并分析出销量最好的十个商家,以饼图的形式展示。通过本篇博客的学习,可以了解到Python在数据爬取和数据分析方面的强大应用。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值