为什么这不能正确运行?有什么想法吗?

这为什么不能正常运行?网站上的价格为80美元,我将其设置为

这是我在python中的第一个程序,因此我对此非常不满意。这是一个由BeautifulSoup制成的网络爬虫。让我知道您是否需要更多信息。 为了保护隐私,我在下面的代码中放置了一封伪造的电子邮件。

import requests
from bs4 import BeautifulSoup
import smtplib
import time

URL = 'https://www.amazon.com/Hubcaps-com-Premium-Quality-Hubcaps- 
Construction/dp/B06XRTYSHH/ref=sr_1_1? keywords=2017+nissan+sentra+hubcaps&qid=1572841095&replacementKeywords=hubcaps&sr=8- 
 1&vehicle=2017-67-899------------&vehicleName=2017+Nissan+Sentra'

headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 
(KHTML,like Gecko) Chrome/77.0.3865.120 Safari/537.36'}


def check_price():
    page = requests.get(URL,headers=headers)

    soup = BeautifulSoup(page.content,'html.parser')

    title = soup.find(span="productTitle").get_text()
    price = soup.find(span="priceblock_ourprice").get_text()
    converted_price = float(price[0:2])

if converted_price < 81:
    send_mail()

print(converted_price)
print(title.strip())
if converted_price < 81:
    send_mail()


def send_mail():
    server = smtplib.SMTP('smtp.gmail.com',587)
    server.ehlo()
    server.starttls()
    server.ehlo()

    server.login(fakeemail@gmail.com','pcsziqijxiebnvun')

subject = 'Price Drop'
body = 'Check it out here: https://www.amazon.com/Hubcaps-com-Premium-Quality-Hubcaps-Construction/dp/B06XRTYSHH/ref=sr_1_1?keywords=2017+nissan+sentra+hubcaps&qid=1572841095&replacementKeywords=hubcaps&sr=8-1&vehicle=2017-67-899------------&vehicleName=2017+Nissan+Sentra'

msg = f"Subject: {subject}\n\n{body}"

server.sendmail(
    'fakeemail@gmail.com','fakeemail@live.com',msg
)
print('EMAIL HAS BEEN SENT!')

server.quit()

while(True):
    check_price()
    time.sleep(60)
y5897411 回答:为什么这不能正确运行?有什么想法吗?

我在代码中看到的错误很少。

首先要解决的问题是:在获得价格之前先检查价格。应该是这样的。

while True:
    price = check_price()

    if price < 81:
        send_mail()

    time.sleep(60)

您还将81与仅存在于函数内部的变量进行比较-您应使用return为外部/全局变量赋值

 def check_price():
     # ...rest...
     return float(price[0:2])

然后您可以

    price = check_price()

    if price < 81:
        send_mail()

我还发现了其他问题。

要获得标题和价格,我必须使用lxml而不是http.parser

soup = BeautifulSoup(response.content,'lxml')

title = soup.find('span',{'id': "productTitle"}).get_text()
price = soup.find('span',{'id': "priceblock_ourprice"}).get_text()

通过这些更改,我发现了另一个问题-您网址中的网页没有价格,因此第二find()给出了None,而我的None.get_text()显示了错误。

在使用find()之前,我必须检查get_text()的结果,

price = soup.find('span',{'id': "priceblock_ourprice"})

if price:
    price = price.get_text()
    price = float(price[0:2])

#if (price is not None) and (price < 81):
if price and price < 81:
    send_mail()

之后,找到价格没有问题。

如果您没有测试这部分代码,我预计gmail只会出现问题-因为目前Google对gmail的访问有更多限制规则,因此您必须转到Google / Gmail帐户并生成特殊密码仅用于脚本的密码(如果您为Gmail设置了“两工厂保护”)。它可能与您用于在Web浏览器中访问Gmail的密码不兼容。


此处包含所有更改的完整代码

import requests
from bs4 import BeautifulSoup
import smtplib
import time

# --- functions ---

def check_price(URL):

    headers = {
        "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/77.0.3865.120 Safari/537.36'
    }

    response = requests.get(URL,headers=headers)

    soup = BeautifulSoup(response.content,'lxml')

    title = soup.find('span',{'id': "productTitle"})
    if title:
        title = title.get_text()
        title = title.strip()
    print('title:',title)

    price = soup.find('span',{'id': "priceblock_ourprice"})
    if price:
        price = price.get_text()
        price = float(price[0:2])
    print('price:',price)

    return title,price


def send_mail(URL,title,price):

    server = smtplib.SMTP('smtp.gmail.com',587)
    server.ehlo()
    server.starttls()
    server.ehlo()

    server.login('fakeemail@gmail.com','pcsziqijxiebnvun')

    subject = 'Price Drop'
    body = f'Check it out here: {URL}\nTitle: {title}\nPrice: {price}'

    msg = f"Subject: {subject}\n\n{body}"

    server.sendmail(
        'fakeemail@gmail.com','fakeemail@live.com',msg
    )
    print('EMAIL HAS BEEN SENT!')

    server.quit()

# --- main ---

URL = 'https://www.amazon.com/Hubcaps-com-Premium-Quality-Hubcaps-Construction/dp/B06XRTYSHH/ref=sr_1_1? keywords=2017+nissan+sentra+hubcaps&qid=1572841095&replacementKeywords=hubcaps&sr=8-1&vehicle=2017-67-899------------&vehicleName=2017+Nissan+Sentra'

while True:
    title,price = check_price(URL)

    if price and price < 81:
        send_mail(URL,price)

    print('sleep')
    time.sleep(60)
本文链接:https://www.f2er.com/3162564.html

大家都在问