这为什么不能正常运行?网站上的价格为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)