首页 > 解决方案 > 部署一个简单的 Python 机器人

问题描述

代码:

    import requests as rq
    from bs4 import BeautifulSoup as bs

    url = "https://apod.nasa.gov/apod/astropix.html"
    page = rq.get(url).content
    soup = bs(page, 'html.parser')
    response = soup.find('img')
    if response == None:
        imglink = soup.find('iframe')['src']
    else:
        imglink = 'https://apod.nasa.gov/apod/' + response['src']
    def main():
        sess = rq.Session()
        cid='**************'
        turl = 'https://api.telegram.org/bot*******************/'
        if response == None:
            imglink = soup.find('iframe')['src']
            params = {'chat_id':cid,'text':imglink}
            sess.post(turl + 'sendMessage', data=params)
        else:
            imglink = 'https://apod.nasa.gov/apod/' + response['src']
            title = soup.find('b').get_text()
            params = {'chat_id':cid,'photo':imglink,'caption':title}
           sess.post(turl + 'sendPhoto', data=params)

    if __name__ == '__main__':
        main()

这是一个简单的机器人,用于将 Nasa 图片发送到我的电报频道。我将修改此脚本以使其每天都发生。但他的问题是我在哪里托管它们,这样它就会一直运行(免费)。正确的做法是什么。

标签: pythondeploymenttelegram

解决方案


我不知道有任何提供商会免费提供此服务。为了便宜,AWS 和 Google Cloud 都有简单的解决方案。

例如:https ://cloud.google.com/blog/products/application-development/how-to-schedule-a-recurring-python-script-on-gcp


推荐阅读