【发布时间】:2019-06-25 21:25:28
【问题描述】:
我正在使用 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+ 运行的,如果这有帮助的话。
有什么线索吗?
【问题讨论】:
-
您如何发现您的 cron 运行脚本没有按照您的预期执行?它是否在某处记录错误?如果是这样,请发布它们!如果您的脚本(如您展示的示例)仅产生打印输出,如果 cron 在未连接到控制台的情况下运行它,您可能看不到它。
-
给我们更多线索如何继续? “它不会工作”是什么意思?这会产生什么输出?你怎么知道它正在运行?也许你的 cron 配置是错误的。如果您还没有,您可以做的一件事是编写您的 cron 命令行以将 stdout 和 stderr 重定向到文件。然后你可以看到 a) 代码正在运行,b) 它正在输出什么。该输出对于诊断这一点可能至关重要。这里有很多事情可能是错误的。根据您展示和告诉我们的情况,我们只能做出疯狂的猜测。
-
照原样,如果您的代码实际上正在运行并且您不知道它在做什么,您可能会在系统日志或日志中找到有用的信息。 - 请注意 cron 以什么用户身份运行您的代码,并确保所有相关权限都正确,以便代码以该用户身份正常运行。
-
Cron 不提供与登录 shell 或 Selenium 相同的环境变量。不要依赖PATH,调用命令时使用绝对路径。
-
@sombra2:抱歉,我说的不是脚本中的变量,而是 shell 正确运行 python 脚本所需的环境变量。