如何让Twilio发短信给我,如果一个产品im监控是在s

2024-03-29 07:55:33 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我目前正在监测一个网站上的特定产品。我可以让twilio发短信给我只要运行脚本,但它不会更新我,如果一个产品是instock或没有。以下是与此过程相关的代码:

import requests,json,re,time,sys
import twilio
from bs4 import BeautifulSoup
from twilio.rest import Client


timeout = [] #Define global timeout list, this list is used to blacklist pops once they've sent one notification so you don't get 100 notifications for an in stock pop.

def url_to_html(url):
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')
    return soup


def hottopic_stock(url):
    soup = url_to_html(url)
    html_source = soup.find_all("div", {"class" : "availability-msg"})
    match = re.search(r'\bIn Stock\b',str(html_source))
    if match: #Return true if In Stock
        return False


def CheckFunko(Site, Title, url):
    global timout
    print("Checking: "+Site+" "+Title+" "+url)

#this is where I want to combine the functions
    if Site == 'Hot Topic':
        status = hottopic_stock(url)
    else:
        status = False
    if status == True:
        client = Client("Api key, twilio account number")
        client.messages.create(to="My phone number",
                               from_="Twillo Phone number",
                               body="Duffer Brothers in stock!"

Tags: tofromimporturlnumberif产品def
1条回答
网友
1楼 · 发布于 2024-03-29 07:55:33

问题似乎出在hottopic_stock函数上,因为它只返回False(如果regex匹配)或None(如果regex不匹配),而在发送代码时检查状态是否为True,这永远不会是这种情况。也许可以试试这个(我假设这个东西有存货,如果regex匹配并且没有测试这个代码的话):

def hottopic_stock(url):
    soup = url_to_html(url)
    html_source = soup.find_all("div", {"class" : "availability-msg"})
    match = re.search(r'\bIn Stock\b',str(html_source))
    return match is not None

相关问题 更多 >