我试图创建一个程序,当产品价格低于 200 美元时发送电子邮件,但它不起作用并给我这个错误:
line 22, in <module>
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("[email protected]", "my_password")
connection.sendmail(
from_addr= "[email protected]",
to_addrs= "[email protected]",
msg =f"Subject:Amazon Price Alert!\n\n{message}\n{url}".encode("utf-8",))
我期待它给我发一封电子邮件,其中写着:产品价格现在是 {product_price},那么你认为我必须为该程序做什么
实际上,当您点击 URL 时,亚马逊弹出窗口会显示接受 cookie,所以我只是添加了它。
参考文献:使用Python(漂亮的汤)抓取一个需要单击“我同意cookie”按钮的网页?
import requests
from bs4 import BeautifulSoup
url = "https://www.amazon.com/dp/B075CYMYK6?ref_=cm_sw_r_cp_ud_ct_FM9M699VKHTT47YD50Q6&th=1"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers=headers, cookies={'__hs_opt_out': 'no'})
soup = BeautifulSoup(response.content, "lxml")
price = soup.find('span', class_='a-price-whole').get_text()
title = soup.find('span', id='productTitle').get_text()
print(price)
print(title)