很高兴有人能提供帮助。
python 脚本 (a.py) 从终端插入 Mysql (mydatabase/table1) 时运行良好,但 mysql (mydatabase/table1) 在作为 cron 作业运行时无法插入或更新。
a.py解析documents目录下所有*.htm文件,结果lst_url插入mydatabase/table1字段lst_url。
a.py 位于 documents 目录下。
cron 作业是 * * * * * documents/a.py
提前致谢。
东
a.py
-------------------------------------------------
import time
from bs4 import BeautifulSoup
from datetime import datetime
import pymysql
import glob
import os
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root',passwd='XXXXXXXX',db='mysql', charset='utf8')
cur=conn.cursor()
cur.execute("USE mydatabase")
for page in glob.glob('documents/*.htm'):
with open(page) as html_file:
soup = BeautifulSoup(html_file, 'lxml')
try:
with conn.cursor() as cursor:
for item in soup.find_all('div',class_='listing_info'):
lst_url= item.find('a', href=re.compile(r'[/]([a-z]|[A-Z])\w+')).attrs['href'
sql="INSERT INTO mydatabase.table1(lst_url) VALUES (%s)"
cur.execute(sql,(lst_url))
conn.commit()
finally:
#conn.close()
print("Done")
最佳答案
Cron 可能在不同的 Python 版本/环境中运行它。我会按照 Spinor8 和 Dan-Dev 所说的去做,我会解释原因。
正如 Spinor8 所说,您可以在程序开头记录 python verison。 Here's a great link explaining how.
正如 Dan-Dev 所说,您应该定义要与 shebang 一起使用的 python 的路径。
关于python - MAC | Python 脚本运行良好,在终端插入 Mysql 表但无法作为 Cron 作业运行 |苹果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51800216/