热销商品-爬虫销量信息

技术部分详解

1、发送GET请求:使用requests库的get()方法发送GET请求来获取指定网页的内容。在这个例子中,使用了一个自定义的User-Agent,以模拟一个浏览器发送请求。

复制代码
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}

Mozilla/5.0:通用格式标识,表示这是一个Mozilla浏览器的用户代理。

(Windows NT 10.0; Win64; x64):操作系统和系统架构的说明,表示这是Windows NT 10.0操作系统的64位版本。

AppleWebKit/537.36 (KHTML, like Gecko):WebKit引擎的版本信息,类似于Chrome浏览器的引擎。
Chrome/58.0.3029.110 Safari/537.3:浏览器的版本信息,这里表示Chrome浏览器的58.0.3029.110版本和Safari浏览器的537.3版本。

复制代码
# 发送GET请求获取页面内容
    response = requests.get(url, headers=headers)

2、解析页面内容:使用BeautifulSoup库的BeautifulSoup()方法来解析页面的HTML内容。在这个例子中,使用了'html.parser'作为解析器来解析网页。

复制代码
# 使用BeautifulSoup解析页面内容
    soup = BeautifulSoup(response.content, 'html.parser')

3、查找元素:通过调用BeautifulSoup对象的find_all()方法来查找所有符合指定条件的元素。在这个例子中,使用了class属性为'tt'的div元素。

复制代码
# 查找所有<div class="tt">元素
    items = soup.find_all('div', class_='tt')

4、收集数据:通过循环迭代每个找到的元素并提取所需的信息来收集数据。在这个例子中,利用get_text()方法获取元素的文本内容,并使用正则表达式对文本进行处理和分割,以得到产品名称和销量数据。

完整代码如下:

复制代码
import requests
from bs4 import BeautifulSoup
import re

def scrape_and_collect_data(url, num_items):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
        }
    
    # 发送GET请求获取页面内容
    response = requests.get(url, headers=headers)
    
    # 使用BeautifulSoup解析页面内容
    soup = BeautifulSoup(response.content, 'html.parser')
    
    # 查找所有<div class="tt">元素
    items = soup.find_all('div', class_='tt')
    
    product_names = []
    sales_data = []
    
    # 收集数据
    for index, item in enumerate(items):
        if index >= num_items:
            break
        
        item_text = item.get_text(strip=True)
        item_text_without_original_price = re.sub(r'原价.*', '', item_text)  # 删除包括"原价"后的文本
        sales_div = item.find_next('div', class_='ss')
        
        if sales_div:
            # 提取销量信息并去除括号及其内部内容
            sales_info = re.sub(r'\([^()]*\)', '', sales_div.get_text(strip=True))
            sales_info_cut = sales_info.split('热销')[-1].split('件')[0].strip()  # 切片操作提取券后之后到'-'之前的内容
            product_names.append(item_text_without_original_price)
            sales_data.append(int(sales_info_cut))
            print(item_text_without_original_price)
            print(sales_info_cut)
    
    return product_names, sales_data

# 要爬取的网页链接和指定爬取的条数
url = 'https://tophub.today/c/shopping'
num_items = 10  # 指定爬取前10条数据

# 调用函数执行爬取并收集数据
product_names, sales_data = scrape_and_collect_data(url, num_items)
相关推荐
小白学大数据10 小时前
构建1688店铺商品数据集:Python爬虫数据采集与格式化实践
开发语言·爬虫·python
AI分享猿11 小时前
免费WAF天花板!雷池WAF护跨境电商:企业级CC攻击防御,Apache无缝适配
爬虫·web安全
雪碧聊技术14 小时前
手刃一个爬虫小案例
爬虫·第一个爬虫案例
野生工程师16 小时前
【Python爬虫基础-1】爬虫开发基础
开发语言·爬虫·python
嫂子的姐夫19 小时前
21-webpack介绍
前端·爬虫·webpack·node.js
Pocker_Spades_A1 天前
Python快速入门专业版(五十四):爬虫基石:HTTP协议全解析(从请求到响应,附Socket模拟请求)
爬虫·python·http
B站计算机毕业设计之家2 天前
Python招聘数据分析可视化系统 Boss直聘数据 selenium爬虫 Flask框架 数据清洗(附源码)✅
爬虫·python·selenium·机器学习·数据分析·flask
傻啦嘿哟2 天前
用Redis实现爬虫URL去重与队列管理:从原理到实战的极简指南
数据库·redis·爬虫
雪碧聊技术2 天前
爬虫是什么?
大数据·爬虫·python·数据分析
小白学大数据3 天前
集成Scrapy与异步库:Scrapy+Playwright自动化爬取动态内容
运维·爬虫·scrapy·自动化