使用 cron 作业启动 python 脚本

时间:2021-03-27 15:33:18

标签: python cron screen-scraping

我最近尝试在我的 mac 上为我的 python 脚本使用 cron 作业,但它无法正常工作。我有以下简单的脚本来抓取黄金价格,将价格和时间标记为 csv。

import csv
import requests
import bs4
from datetime import datetime as dt
import os

# task is to scrape gold price and time from https://markets.businessinsider.com/commodities/gold-price and save it in csv
def get_gold_price():
    page = requests.get("https://markets.businessinsider.com/commodities/gold-price")
    soup = bs4.BeautifulSoup(page.text, "html.parser")
    price = soup.find("span", {"class":"price-section__current-value"}).text
    print('The current price is {} USD'.format(price))
    return price

def write_to_csv(price):
    DATE_FORMAT = '%d.%m.%Y %H:%M'
    if "gold_price.csv" in os.listdir():
        mode = "a"
    else:
        mode = "w"
    f = open("gold_price.csv", mode)
    f_writer = csv.writer(f)
    f_writer.writerow([price,dt.now().strftime(DATE_FORMAT)])
    f.close()
print(write_to_csv(get_gold_price()))

当我在 Pycharm 中运行脚本时,它运行良好。当我在终端中放入我的 crontab 时,它不会每分钟都标记价格。

* * * * * /Users/petrsevcik/PycharmProjects/New_Scraping_Project/bin/python /Users/petrsevcik/PycharmProjects/scraping_gold_price/Scraping_Gold_Price.py

有什么建议吗?是 cron 的问题还是我的脚本的问题?

0 个答案:

没有答案