You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用BeautifulSoup爬取所有专辑名称与价格

问题解决:获取网站全部专辑名称和价格

原代码的问题在于:find_all('ul', class_='products columns-3')仅返回页面中唯一的产品列表容器,后续循环里用find()方法只能提取该容器内第一个产品的标题和价格,因此只能拿到一组数据。

修改后的代码如下:

import requests
from bs4 import BeautifulSoup

URL = 'https://vinilosalvaro.cl/tienda/'
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'}
page = requests.get(URL, headers=headers)

soup = BeautifulSoup(page.content, 'html.parser')
# 定位到唯一的产品列表容器
product_container = soup.find('ul', class_='products columns-3')
# 遍历容器内的每个产品项
for product in product_container.find_all('li', class_='product'):
    # 提取当前产品的标题
    title = product.find('h2', class_='woocommerce-loop-product__title').text.strip()
    # 提取当前产品的价格
    price = product.find('span', class_='woocommerce-Price-amount amount').text.strip()
    print(f"专辑名称: {title}")
    print(f"价格: {price}\n")

核心修改点:

  • 用find()替代find_all()定位产品列表容器(页面仅存在一个该类别的ul)
  • 遍历容器内的每个li.product元素,每个li对应一个独立的专辑产品
  • 在每个产品节点内单独提取标题和价格,确保获取所有产品数据

内容的提问来源于stack exchange,提问作者Gustavo Guzmán

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 05:50:57