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

如何用BeautifulSoup提取亚马逊商品li标签下的厂商与型号信息

解决亚马逊印度站商品厂商和型号爬取问题

你的问题出在直接匹配包含特殊空白字符(换行、零宽空格)的原始字符串——亚马逊页面的标签文本里有很多不可见的Unicode控制字符(比如U+200F、U+200E)和多余换行,导致精确匹配失败。以下是两种可行的爬取方案:

方案一:清理文本后匹配标签

遍历详情区域的每个列表项,提取标签和对应值,通过正则清理文本中的冗余字符后匹配目标字段:

from bs4 import BeautifulSoup
import requests
import re

# 目标商品URL
url = "https://www.amazon.in/Sparx-SM-687-Forest-Golden-SX0687GGFGO_0009/dp/B098BC48PZ/ref=sr_1_73?keywords=mens%2Bshoes&qid=1674804026&sr=8-73&th=1&psc=1"
# 必须添加请求头,避免亚马逊反爬拦截
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}

response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')

product_info = {}
detail_area = soup.find('div', id='detailBullets_feature_div')

if detail_area:
    for li in detail_area.find_all('li'):
        spans = li.find_all('span')
        if len(spans) >= 2:
            # 清理标签文本:移除多余空格、换行、Unicode控制字符
            label = re.sub(r'\s+|[\u200f\u200e]', ' ', spans[0].get_text()).strip()
            value = spans[1].get_text().strip()
            
            if 'Manufacturer' in label:
                product_info['Manufacturer'] = value
            elif 'Item Model Number' in label:
                product_info['Item Model Number'] = value

print(product_info)

方案二:使用lambda函数模糊匹配标签

通过lambda函数判断span文本是否包含目标关键词,无需精确匹配所有空白字符,直接定位目标标签并提取对应值:

from bs4 import BeautifulSoup
import requests

url = "https://www.amazon.in/Sparx-SM-687-Forest-Golden-SX0687GGFGO_0009/dp/B098BC48PZ/ref=sr_1_73?keywords=mens%2Bshoes&qid=1674804026&sr=8-73&th=1&psc=1"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}

response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')

# 提取厂商信息
manufacturer_tag = soup.find('span', text=lambda t: t and 'Manufacturer' in t.strip())
manufacturer = manufacturer_tag.find_next_sibling('span').get_text().strip() if manufacturer_tag else None

# 提取型号信息
model_tag = soup.find('span', text=lambda t: t and 'Item Model Number' in t.strip())
model_number = model_tag.find_next_sibling('span').get_text().strip() if model_tag else None

print(f"厂商: {manufacturer}, 型号: {model_number}")

注意事项

  • 必须添加User-Agent请求头,否则亚马逊会返回反爬页面(如验证码或503错误),导致无法解析内容。
  • 如果遇到验证码拦截,可能需要添加代理或使用会话保持,或考虑使用亚马逊官方API(若有访问权限)。

内容的提问来源于stack exchange,提问作者Lalit Joshi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 18:05:48