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

亚马逊价格追踪器报AttributeError,请求故障排查与修复

亚马逊价格追踪程序故障排查与修复

问题描述

我开发了一款亚马逊价格追踪程序,预期当商品价格低于200美元时发送邮件提醒,但程序运行失败,抛出如下错误:

line 22, in
title = soup.find(id="Electric Pressure Cookers").get_text().strip()
AttributeError: 'NoneType' object has no attribute 'get_text'

我的代码如下:

import requests
import lxml
import smtplib
from bs4 import BeautifulSoup

url = "https://www.amazon.com/dp/B075CYMYK6?ref_=cm_sw_r_cp_ud_ct_FM9M699VKHTT47YD50Q6&th=1"
header = {
    "User-Agent": "my_user_agent",
    "Accept-Language": "my_accept_language",
}

response = requests.get(url, headers=header)

soup = BeautifulSoup(response.content, "lxml")
print(soup.prettify())

price = soup.find(class_="a-offscreen").get_text()
price_without_currency = price.split("$")[1]
price_as_float = float(price_without_currency)
print(price_as_float)

title = soup.find(id='electrical cooker').get_text().strip()
print(title)

BUY_PRICE = 200

if price_as_float < BUY_PRICE:
    message = f"{title} is now {price}"

    with smtplib.SMTP("smtp.gmail.com", port=587) as connection:
        connection.starttls()
        result = connection.login("my_gmail@gmail.com", "my_password")
        connection.sendmail(
            from_addr= "my_gmail@gmail.com",
            to_addrs= "my_gmail@gmail.com",
            msg =f"Subject:Amazon Price Alert!

{message}
{url}".encode("utf-8",))

我原本期望程序发送内容为‘商品价格为{product_price}’的提醒邮件,请问该如何修改才能让程序正常运行?

错误原因

  1. 元素定位错误:你用soup.find(id='electrical cooker')查找标题,但亚马逊商品页的标题元素实际id是productTitle,错误的选择器导致find返回None,调用get_text()时触发AttributeError。
  2. Headers占位符未替换:header里的User-Agent和Accept-Language是占位符,未填写真实值,亚马逊可能返回反爬虫页面或结构异常的页面,导致无法解析元素。
  3. 邮件格式不规范:发送邮件的msg部分缺少必要的换行分隔,不符合SMTP协议格式,可能导致邮件发送失败。

修复方案

1. 修正标题元素定位

将标题查找代码替换为:

title = soup.find(id="productTitle").get_text().strip()

2. 替换真实的Headers信息

打开浏览器访问亚马逊,按F12打开开发者工具,切换到「Network」标签,刷新页面后找到第一个请求,查看「Request Headers」中的User-Agent和Accept-Language,将其替换到代码的header字典中。

3. 增加异常处理

为避免元素查找失败导致程序崩溃,添加判断逻辑:

# 处理价格获取异常
price_element = soup.find(class_="a-offscreen")
if not price_element:
    print("无法获取商品价格,请检查Headers或页面结构")
    exit()
price = price_element.get_text()

# 处理标题获取异常
title_element = soup.find(id="productTitle")
if not title_element:
    print("无法获取商品标题,请检查Headers或页面结构")
    exit()
title = title_element.get_text().strip()

4. 规范邮件格式

邮件内容需符合SMTP格式,在主题与正文之间添加空行:

msg = f"Subject:Amazon Price Alert!\n\n{message}\n{url}".encode("utf-8")

完整修改后的代码

import requests
import smtplib
from bs4 import BeautifulSoup

url = "https://www.amazon.com/dp/B075CYMYK6?ref_=cm_sw_r_cp_ud_ct_FM9M699VKHTT47YD50Q6&th=1"
# 替换为你自己的真实Headers信息
header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
    "Accept-Language": "en-US,en;q=0.9",
}

response = requests.get(url, headers=header)
soup = BeautifulSoup(response.content, "lxml")

# 获取价格并处理异常
price_element = soup.find(class_="a-offscreen")
if not price_element:
    print("无法获取商品价格,请检查Headers或页面结构")
    exit()
price = price_element.get_text()
price_without_currency = price.split("$")[1]
price_as_float = float(price_without_currency)
print(f"当前价格:{price_as_float}美元")

# 获取标题并处理异常
title_element = soup.find(id="productTitle")
if not title_element:
    print("无法获取商品标题,请检查Headers或页面结构")
    exit()
title = title_element.get_text().strip()
print(f"商品标题:{title}")

BUY_PRICE = 200

if price_as_float < BUY_PRICE:
    message = f"{title} 现在价格为 {price}"
    print("价格低于阈值,准备发送提醒邮件")

    try:
        with smtplib.SMTP("smtp.gmail.com", port=587) as connection:
            connection.starttls()
            # 替换为你的Gmail账号和应用专用密码(Gmail需开启两步验证并使用应用密码)
            connection.login("your_gmail@gmail.com", "your_app_password")
            msg = f"Subject:亚马逊价格提醒!\n\n{message}\n商品链接:{url}".encode("utf-8")
            connection.sendmail(
                from_addr="your_gmail@gmail.com",
                to_addrs="your_gmail@gmail.com",
                msg=msg
            )
        print("邮件发送成功")
    except Exception as e:
        print(f"邮件发送失败:{str(e)}")

内容的提问来源于stack exchange,提问作者Eren Evli

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 13:05:33