我的脚本在控制台上运行时有效,但在cron(?)上调度时却无效

时间:2019-06-23 05:34:35

标签: python selenium cron

我正在使用selenium运行以下代码,当我在控制台上对其进行测试时,它可以完美运行。但是当我出于某种原因在Cron上安排它时,它将无法工作。硒有问题吗?与克朗?我在这里想念什么?

import tweepy
from selenium import webdriver
import datetime

now = datetime.datetime.now()
now = '{}/{}/{} {}:{}'.format(now.day, now.month, now.year, now.hour, now.minute)


d = webdriver.Chrome(r'cromewebdriver')
d.get('https://deuda-publica-espana.com/')
deuda_total = d.find_element_by_class_name('contador_xxl').text
deuda_hab = d.find_element_by_class_name('contador_xl').text
d.quit()



deuda_hab = deuda_hab.split(',')
deuda_hab = deuda_hab[0]



PIB = 1208248000000

porcentaje = round((int(deuda_sin_puntos) * 100)/PIB, 2)
porcentaje = str(porcentaje).replace('.',',')



print(deuda_total)
print(deuda_hab)
print(str(porcentaje) + '%')

# Now the part where we send the tweet

consumer_key = 'key'
consumer_secret = 'key'
access_token = 'key'
access_token_secret = 'key'

# authentication of consumer key and secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

# authentication of access token and secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

api.update_status(status = '{} € total | '
                           '{} € por habitante | '
                           '{}% del PIB | '
                           '{}'.format(deuda_total, deuda_hab, porcentaje, now))

我在上面显示的内容还有很多,因为这应该在Twitter上发布结果,但是无论如何,由于某种原因,这在cron上将不起作用。

作为参考,该脚本在Raspbian上的Raspberry Pi 3+上运行,如果有帮助的话。

有任何线索吗?

2 个答案:

答案 0 :(得分:0)

尝试使用--headless选项运行chrome webdriver。您将无法通过cron作业运行交互式应用程序或带有gui的应用程序。

答案 1 :(得分:0)

已修复。在crontab文件中添加'export DISPLAY =:0'修复了该问题。 Selenium试图执行,但找不到显示。