如何解决亚马逊价格跟踪器和推送通知
作为我的第二个python项目,我正在尝试使用3行额外的代码(而不是电子邮件)为桌面推送通知(https://www.youtube.com/watch?v=Bg9r_yLk7VY)构建此亚马逊价格跟踪器(https://www.youtube.com/watch?v=KshTf2A5aUk)。运行代码时,我没有收到错误,但也没有收到推送通知。你有什么主意吗?
我的代码如下:
import requests
from bs4 import BeautifulSoup
import win10toast
URL = 'https://www.amazon.de/WOTR-Elektro-Skateboard-Off-Road-vierr%C3%A4drige-Elektroroller-Fernbedienung/dp/B07ZX6TV79/ref=sr_1_25?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=e+skateboard&qid=1597239289&sr=8-25'
headers = {"User Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0'}
def check_price():
page = requests.get(URL,headers=headers)
soup = BeautifulSoup(page.content,'html.parser')
title = soup.find(id="productTitle").get_text()
price = soup.find(id="priceblock_ourprice").get_text()
converted_price = float(price[0:4])
if(converted_price > 700):
send_alert()
print(converted_price)
print(title.strip())
def send_alert():
toaster = win10toast.ToastNotifier().show_toast("Python",'Cheaper',duration=5)
解决方法
您需要调用 check_price()
函数才能运行代码,并且 converted_price = float(price[0:4])
这一段代码也会给您带来错误,您应该使用 split()
进行此操作,因为 price[0:3]
适用于 100-999 如果 999 + ,则会转换错误的价格。
下面是修改后的代码:
import requests
from bs4 import BeautifulSoup
import win10toast
import time
URL = 'https://www.amazon.de/WOTR-Elektro-Skateboard-Off-Road-vierr%C3%A4drige-Elektroroller-Fernbedienung/dp/B07ZX6TV79/ref=sr_1_25?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=e+skateboard&qid=1597239289&sr=8-25'
headers = {"User Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0'}
def check_price():
page = requests.get(URL,headers=headers)
soup = BeautifulSoup(page.content,'html.parser')
title = soup.find(id="productTitle").get_text()
price = soup.find(id="priceblock_ourprice").get_text()
converted_price = float(price.split(",")[0])
print(converted_price)
if(converted_price > 700):
send_alert()
print(converted_price)
print(title.strip())
def send_alert():
toaster = win10toast.ToastNotifier().show_toast("Python",'Cheaper',duration=5)
while True:
check_price()
time.sleep(5) #sleep 5 seconds before requesting the price again
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。