Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用BeautifulSoup在Python中创建不同的网站以获取价格跟踪_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

使用BeautifulSoup在Python中创建不同的网站以获取价格跟踪

使用BeautifulSoup在Python中创建不同的网站以获取价格跟踪,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我是Python的初学者,已经开始使用Python进行web抓取。目前,我正试图为像亚马逊这样的在线商店建立一个价格跟踪器。我可以通过BeautifulSoup和请求从Amazon上获取产品的价格和名称等文本,收集这些数据后,我可以将价格与用户设置的阈值价格进行比较。如果价格低于门槛价格,webscraper会发送电子邮件通知我。我有一个其他在线商店/网站的列表,我想使用我的价格跟踪器,例如Footlocker。由于每个网站都有不同的html结构,有没有一种方法可以编写一个简单的Beautifu

我是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()
导入请求
从bs4导入BeautifulSoup
导入smtplib
URL=input('您想查看哪种亚马逊产品的价格(查看价格)(请使用URL)?)
阈值=输入(“产品的阈值金额是多少?”)
阈值=浮动(阈值)
def check_price():
headers={“用户代理”:}
page=requests.get(URL,headers=headers)
soup=BeautifulSoup(page.content,'html.parser')
title=soup.find(id=“productTitle”).get_text()
price=soup.find(id=“priceblock\u ourprice”).get\u text()
转换后的价格=浮动(价格[1:])
如果(转换价格<阈值):
发送电子邮件()
打印(title.strip())
打印(换算价格)
其他:
打印(f‘您的产品价格未低于${threshold}’)
打印({title.strip()}的当前价格为{converted_price}'))
def send_email():
server=smtplib.SMTP('SMTP.gmail.com',587)
server.ehlo()
server.starttls()
server.login(“”,“”)
主题=f“你方产品的价格已经下跌”
body=f'检查亚马逊链接-{URL}'
msg=f“主题:{Subject}\n\n{body}”
server.sendmail(
'',
'',
味精
)
打印('电子邮件已发送')
server.quit
核对价格

每个网站都有自己的细节。您可以为您的刮片构建一个通用类,该类涵盖所有类似的任务,例如发送电子邮件等。
然后,为每个网站创建一个childclass并覆盖网站特定的功能。

我希望您需要对每个网站进行刮取。BS是灵活的,但是每个网站都有非常不同的HTML结构。每个结构都需要一些代码来提取你想要的内容。我面临着同样的问题,因为每个网站都有不同的HTML结构,一般的刮刀会有什么不同structures@flow_me_over,一个普通的替罪羊怎样才能刮到不同的建筑??有没有可能在没有父类和子类的情况下刮取不同的结构?谢谢@flow\u me\u over,我只是想知道,为了覆盖网站特定的功能,我是否只需要在子类中定义与父类/基类中相同的方法?另外,如果我在我的代码中实现了这一点,是否有任何可能的方法使web scraper价格跟踪两个不同的选择的网站在同一时间与一个代码?你的第一个问题:是的。如果愿意,它可以在父类中完全为空。然后在子类中,使用相同的名称。至于你的第二个问题:我认为你必须研究多线程。或者写一个循环,一个接一个地刮网站OK谢谢你的帮助@flow\u me\u over