如何用python编写一个不间断的爬虫并在服务器上运行它?

Bar*_*ari 6 python ubuntu service web-scraping

我想用python编写一个抓取器,抓取一些网址并抓取并保存数据.我知道怎样才能把它写成一个简单的程序.我正在寻找一种方法将其部署在我的虚拟服务器(运行ubuntu)作为服务,以使其不间断爬行.谁能告诉我怎么办?

Sle*_*aur 5

您想要做的是守护进程。 这将有助于创建守护程序。

守护进程将使其能够在后台模式下运行,因此只要服务器正在运行(即使用户未登录),它将继续运行。

这是将时间写入文件的示例守护程序。

import daemon
import time

def do_something():
    while True:
        with open("/tmp/current_time.txt", "w") as f:
            f.write("The time is now " + time.ctime())
        time.sleep(5)

def run():
    with daemon.DaemonContext():
        do_something()

if __name__ == "__main__":
    run()
Run Code Online (Sandbox Code Playgroud)