【问题标题】:Comparing BeautifulSoup results比较 BeautifulSoup 结果
【发布时间】:2019-07-29 02:11:16
【问题描述】:

我正在买房子,我已经构建了一个 BeautifulSoup 刮刀,它的作用就像一个魅力 - 从我们当地的房地产网站上刮下我需要的标签。现在我只需要实现一个机制,当网站有变化时会通知我。

一旦新的抓取输出与前一个不同 - 当 HTML 更改时,我需要它通知我。

previous_content = ''
URL = 'whatever.com'

while True:
    content = requests.get(URL)
    soup = BeautifulSoup(content.text, 'html.parser')
    titles = soup.find_all('span', attrs={'class':['title']})
    years = soup.find_all('span', attrs={'class':['year']})
    sizes = soup.find_all('span', attrs={'class':['size']})
    prices = soup.find_all('span', attrs={'class':['price']})

    for titles, years, sizes, prices in zip(titles, years, sizes, prices):
        print('Location: ' + titles.get_text(strip="True") + '\n' + 'Year: ' + years.get_text(strip="True"), '\n' + 'Size: ' + sizes.get_text(strip="True"), '\n' 'Price: ' + prices.get_text(strip="True"))
        previous_content = new_content
    if previous_content == new_content:
        print("CONTENT NOT CHANGED. | " + str(today))
    elif previous_content != new_content:
        print("CONTENT CHANGED | " + str(today))
    time.sleep(sleeptime)

非常感谢!

【问题讨论】:

  • 把这些数据保存到数据库中,然后编写一个程序来检查数据库中是否存在新数据
  • 还有监控网站变化的服务
  • 您可以订阅网站的RSS提要

标签: python beautifulsoup


【解决方案1】:

我认为您在分配 previous_content 时犯了一个小错误

您应该在 while 迭代结束时分配 previous_content,而不是在使用 new_content 测试相等性之前分配,否则它将始终为 True

类似的东西应该可以工作(我无法测试)

previous_content = []
URL = 'whatever.com'

while True:
    content = requests.get(URL)
    soup = BeautifulSoup(content.text, 'html.parser')
    titles = soup.find_all('span', attrs={'class':['title']})
    years = soup.find_all('span', attrs={'class':['year']})
    sizes = soup.find_all('span', attrs={'class':['size']})
    prices = soup.find_all('span', attrs={'class':['price']})

    new_content = [] # Initialize the new_content list
    for titles, years, sizes, prices in zip(titles, years, sizes, prices):
        content = 'Location: ' + titles.get_text(strip="True") + '\n' + 'Year: ' + years.get_text(strip="True"), '\n' + 'Size: ' + sizes.get_text(strip="True"), '\n' 'Price: ' + prices.get_text(strip="True")
        print(content)
        new_content.append(content)
    if sorted(previous_content) == sorted(new_content): # The list needs to be sorted as I expect the order to change but not the content
        print("CONTENT NOT CHANGED. | " + str(today))
    else:
        print("CONTENT CHANGED | " + str(today))
    previous_content = new_content # Assigning for next iteration of the loop
    time.sleep(sleeptime)

【讨论】:

  • 感谢蓝羊的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
相关资源
最近更新 更多