【问题标题】:Amazon Price Tracker and Push notification亚马逊价格跟踪器和推送通知
【发布时间】:2020-08-14 11:47:32
【问题描述】:

作为我的第二个 python 项目,我正在尝试使用 3 行额外代码(https://www.youtube.com/watch?v=KshTf2A5aUk)而不是电子邮件来构建这个亚马逊价格跟踪器(https://www.youtube.com/watch?v=Bg9r_yLk7VY)。当我运行代码时,我没有收到错误,但也没有收到推送通知。你有什么想法吗?

我的代码如下:

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)

【问题讨论】:

    标签: python web screen-scraping


    【解决方案1】:

    你需要调用 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
    

    【讨论】:

    • 谢谢你,但我运行了你的代码,它也不起作用
    • 它在第 31 行给了我一个回溯错误和一个属性错误“回溯(最近一次调用最后一次):文件“Neup.py”,第 31 行,在 check_price() 文件“Neup .py",第 15 行,在 check_price title = soup.find(id="productTitle").get_text() AttributeError: 'NoneType' object has no attribute 'get_text'"
    猜你喜欢
    • 2017-09-05
    • 2017-11-14
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多