【问题标题】:How can I use the output of this code and the Telegram API to alert me of a new entry?如何使用此代码的输出和 Telegram API 来提醒我有新条目?
【发布时间】:2021-09-23 23:05:48
【问题描述】:

我正在尝试使用该 URL 抓取最近发行的电影。但我希望收到每个新版本的通知。我还想设置一个计时器,假设脚本每两个小时检查一次是否有新电影上映。如果有新版本,它将被发送到我的 Telegram 机器人。我还不知道该怎么做。

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen

req = Request("https://www.thenetnaija.com/videos/movies", headers={'User-Agent': 'XYZ/3.0'})
webpage = urlopen(req, timeout=10)
b4 = BeautifulSoup(webpage, "html.parser")
movie_list = b4.find_all("div", {"class" : "video-files"})
for allContainers in movie_list:
    filmName = allContainers.find('img').get('alt')
print(filmName)

【问题讨论】:

    标签: python-3.x web-scraping beautifulsoup python-telegram-bot


    【解决方案1】:

    从电报机器人为您发送通知

    1 - 首先与这个机器人交谈:@userinfobot 获取id

    2 - 从@botfather 获取您的机器人令牌

    3 - 向您的机器人发送任何消息

    (以上步骤只有一次)

    4- 提出获取请求

    https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={notification_text}

    【讨论】:

    • 非常感谢,经过几次尝试,它成功了!
    【解决方案2】:
    import time
    from bs4 import BeautifulSoup
    import requests
    from urllib.request import Request, urlopen
    
    req = Request("https://www.thenetnaija.com/videos/movies", headers={'User-Agent': 'XYZ/3.0'})
    webpage = urlopen(req, timeout=10)
    b4 = BeautifulSoup(webpage, "html.parser")
    movie_list = b4.find_all("div", {"class" : "video-files"})
    
    for allContainers in movie_list:
        filmName = allContainers.find('img').get('alt')
    printed = []
    print(filmName)
    while True:
        if filmName not in printed:
            requests.get("https://api.telegram.org/bot{"insert bot_token here"}/sendMessage?chat_id={"insert chat id here"}&text={}".format(filmName))
            time.sleep(10)
            printed.append(filmName)
    

    非常感谢@Aladdin Jiroun!

    【讨论】:

      猜你喜欢
      • 2014-08-16
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      相关资源
      最近更新 更多