我是Python的初学者,已经开始使用Python进行web抓取。目前,我正试图为像亚马逊这样的在线商店建立一个价格跟踪器。我可以通过BeautifulSoup和请求从Amazon上获取产品的价格和名称等文本,收集这些数据后,我可以将价格与用户设置的阈值价格进行比较。如果价格低于门槛价格,webscraper会发送电子邮件通知我。我有一个其他在线商店/网站的列表,我想使用我的价格跟踪器,例如Footlocker。由于每个网站都有不同的html结构,有没有一种方法可以编写一个简单的BeautifulSoup webscraper,允许用户从一组网站列表中选择要删除的网站?我必须为每个特定的网站写一个吗?。我为URL使用了一个输入,因此用户必须将URL复制粘贴到shell中,然后webscraper将从amazon页面中删除
感谢您阅读本文,如有任何其他反馈,我们将不胜感激
这是我的代码到目前为止没有电子邮件发送电子邮件和我的用户代理
import requests
from bs4 import BeautifulSoup
import smtplib
URL = input('What Amazon product would you like to check the price of(View price)(please use URL)? ')
threshold = input('What is the threshold amount of the product? ')
threshold = float(threshold)
def check_price():
headers = {"User-Agent": }
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[1:])
if (converted_price < threshold):
send_email()
print(title.strip())
print(converted_price)
else:
print(f'The price of your product has not fallen below ${threshold}')
print(f'The current price of the {title.strip()} is {converted_price}')
def send_email():
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('', '')
subject = f'The price of your product has fallen'
body = f'Check the amazon link - {URL}'
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
'',
'',
msg
)
print('The email has been sent')
server.quit
check_price()
每个网站都会有自己的细节。您可以为您的刮片构建一个通用类,该类涵盖所有类似的任务,例如发送电子邮件等。 然后,为每个网站创建一个childclass并覆盖网站特定的功能
相关问题 更多 >
编程相关推荐