首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查新的折扣,如果发现变化,如何发送到电报?

如何检查新的折扣,如果发现变化,如何发送到电报?
EN

Stack Overflow用户
提问于 2021-12-11 10:20:57
回答 1查看 184关注 0票数 1

我喜欢从网站上拿出新的折扣,并在网站上给我发电报和零钱。

这是可行的,但我得到了很多信息,我想改变脚本,以检查网站上的一个特定的class

所以在网站上,我想检查一下<span class="space--ml-1 size--all-l size--fromW3-xl cept-discount">-49%</span>

如果值在-65%到-99%之间,我想要一条消息。这个是可能的吗?下面是检查更改的脚本:

代码语言:javascript
复制
import requests
from bs4 import BeautifulSoup
import difflib
import time
from datetime import datetime
import re
import os
import schedule
import cloudscraper


# target URL
url = "https://nl.pepper.com/groep/prijsfout"
# act like a browser
#headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36         (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
#headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36     (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'}

scraper = cloudscraper.create_scraper()


# Send a message via a telegram bot
def telegram_bot_sendtext(bot_message):
bot_token = '17XXXX32:AAFd5jXXXXXXXXXXXXC5UJgG5pses8'
bot_chatID = '-XXXXX'
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID     + '&parse_mode=Markdown&text=' + bot_message

response = requests.get(send_text)

return response.json()


PrevVersion = ""
FirstRun = True
while True:

# download the page
response = scraper.get("https://nl.pepper.com/nieuw").content
# parse the downloaded homepage
soup =  BeautifulSoup(response, 'html.parser')

# remove all scripts and styles
for script in soup(["script", "style"]):
    script.extract()
soup = soup.get_text()
# compare the page text to the previous version
if PrevVersion != soup:
    # on the first run - just memorize the page
    if FirstRun == True:
        PrevVersion = soup
        FirstRun = False
        print ("Start Monitoring "+url+ ""+ str(datetime.now()))
    else:
        print ("Changes detected at: "+ str(datetime.now()))
        OldPage = PrevVersion.splitlines()
        NewPage = soup.splitlines()
        # compare versions and highlight changes using difflib
        #d = difflib.Differ()
        #diff = d.compare(OldPage, NewPage)
        diff = difflib.context_diff(OldPage,NewPage,n=0)
        out_text = "\n".join([ll.rstrip() for ll in '\n'.join(diff).splitlines() if     ll.strip()])
        print (out_text)
        OldPage = NewPage
        # Send the message (such as with a telegram bot provided below)
        telegram_bot_sendtext("Nieuwe prijsfout op Pepper " + url + out_text )

       # print ('\n'.join(diff))
        PrevVersion = soup
else:
    print( "No Changes "+ str(datetime.now()))
time.sleep(5)
continue

也许这个脚本中的cookie也有问题(或者它没有定义)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-11 12:19:45

如果有任何折扣在-65%到-99%之间,一个简单的可能的解决方案可能是:

此函数接受您的soup,并在一般情况下查找折扣,如果在您的范围内有任何折扣,则返回True;如果没有,则返回False

代码语言:javascript
复制
def get_discounts(soup):
    for d in soup.select('.cept-discount'):
        if d.text != '' and 65 < int(''.join(filter(str.isdigit, d.text))) < 99:
            return True
        else:
            return False
        
get_discounts(soup)

Note在调用soup = soup.get_text()之前调用函数--顺序是将soup的内容更改为文本的关键原因。

最好将文本存储在另一个/重命名的变量中,例如souptext,.因此,您可以肯定,soup始终包含BeautifulSoup object__,它将document表示为嵌套的数据结构。

所以你最终会有这样的结果:

代码语言:javascript
复制
import requests, time, difflib, os, re, schedule, cloudscraper
from bs4 import BeautifulSoup
from datetime import datetime

# target URL
url = "https://nl.pepper.com/groep/prijsfout"
# act like a browser
#headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36         (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
#headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36     (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'}

scraper = cloudscraper.create_scraper()


# Send a message via a telegram bot
def telegram_bot_sendtext(bot_message):
    bot_token = '17XXXX32:AAFd5jXXXXXXXXXXXXC5UJgG5pses8'
    bot_chatID = '-XXXXX'
    send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID     + '&parse_mode=Markdown&text=' + bot_message

    response = requests.get(send_text)

    return response.json()


PrevVersion = ""
FirstRun = True
while True:

    # download the page
    response = scraper.get("https://nl.pepper.com/nieuw").content
    # parse the downloaded homepage
    soup =  BeautifulSoup(response, 'html.parser')

    # remove all scripts and styles
    for script in soup(["script", "style"]):
        script.extract()
    discounts = get_discounts(soup)

    soup = soup.get_text()

    # compare the page text to the previous version and check if there are any discounts in your range

    if PrevVersion != soup and discounts:       
        # on the first run - just memorize the page
        if FirstRun == True:
            PrevVersion = soup
            FirstRun = False
            print ("Start Monitoring "+url+ ""+ str(datetime.now()))
        else:
            print ("Changes detected at: "+ str(datetime.now()))
            OldPage = PrevVersion.splitlines()
            NewPage = soup.splitlines()
            # compare versions and highlight changes using difflib
            #d = difflib.Differ()
            #diff = d.compare(OldPage, NewPage)
            diff = difflib.context_diff(OldPage,NewPage,n=0)
            out_text = "\n".join([ll.rstrip() for ll in '\n'.join(diff).splitlines() if     ll.strip()])
            print (out_text)
            OldPage = NewPage
            # Send the message (such as with a telegram bot provided below)
            telegram_bot_sendtext("Nieuwe prijsfout op Pepper " + url + out_text )

           # print ('\n'.join(diff))
            PrevVersion = soup
    else:
        print( "No Changes "+ str(datetime.now()))
    time.sleep(5)
    continue
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70314322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档