在计划任务中写入文件 Python

Write to file in a schedule task Python

我有一个 python 脚本,每 10 秒抓取一次网络(计划任务),我需要以文件格式保存该数据。问题是我只能保存最后一组数据。我猜其他数据被计划任务覆盖了。

import sched
import time
from bs4 import BeautifulSoup
import requests
import datetime

scheduler = sched.scheduler(time.time, time.sleep)
url = 'https://in.finance.yahoo.com/q?s=AAPL'

def execute_async_task(address):
    requested = requests.get(address)
    data = requested.text
    soup = BeautifulSoup(data, 'html.parser')
    for link in soup.findAll('span', {'id': 'yfs_l84_aapl'})[0]:
        if link:
            f = open('PlotData.txt', 'w')
            f.write("stock_price:"+str(link)+"\n")
            time.sleep(0.05)
            scheduler.enter(10, 1, execute_async_task, (url,))


scheduler.enter(0, 1, execute_async_task, (url,))
scheduler.run()

我比较新,要python。

使用f = open('PlotData.txt', 'a')代替f = open('PlotData.txt', 'w')

'w': 覆盖现有文件

'a':追加到文件中已有的数据