首页 > 代码库 > python常用脚本以及问题跟踪
python常用脚本以及问题跟踪
1.时间操作
//获取当前时间 格式是%Y-%m-%d %H:%M:%S
currTime = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
//获取前一天日期格式为%Y-%m-%d
yesterday = datetime.date.today() - datetime.timedelta(days=1)
//获取前一个小时日期
yesterday = (datetime.datetime.now() - datetime.timedelta(hours = 1)).strftime("%Y-%m-%d %H:%M:%S")
2.mysql 操作
#!/usr/bin/python
#coding=utf-8
import MySQLdb
import datetime
import MailUtil
import time
try:
connection = MySQLdb.connect(user="entdb",passwd="dm6db+1de@v",host="192.168.32.47",db="miss8")
except:
print "Could not connect to MySQL server."
exit( 0 )
print "----------------------------贵圈新闻数据流 check start------------------------------"
mailto_list=[‘xubaolong@iminer.com‘,‘yueyaobiao@iminer.com‘]
try:
while True:
cursor = connection.cursor()
currTime = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
#两小时前时间
two_hours_ago = (datetime.datetime.now() - datetime.timedelta(hours = 2)).strftime("%Y-%m-%d %H:%M:%S")
#明星和电影前一天微指数抓取
cursor.execute( "SELECT count(1) from gquan_article_info where crawl_time>‘"+str(two_hours_ago)+"‘" )
results = cursor.fetchall();
count = 0
for row in results:
count = row[0]
print "INFO\t当前时间: " + str(currTime) + "\t前两小时抓取数量是:" +str(count)
if count == 0:
tag = MailUtil.send_mail(mailto_list,‘贵圈新闻数据流出现问题两小时之内没有数据‘,‘贵圈新闻数据流出现问题,请检查(47 数据库gquan_article_info)请尽快修复!!!!!!‘)
if tag:
print "ERROR\t当前时间:" + str(currTime) + "\t贵圈新闻数据流出现问题\t邮件发送成功"
else:
print "ERROR\t当前时间: " + str(currTime) + "\t贵圈新闻数据流出现问题\t邮件发送失败"
else:
print "INFO\t当前时间:" + str(currTime) + "\t贵圈新闻数据流一切正常\t无须发送邮件"
cursor.close()
print "INFO\t当前时间:" + str(currTime) + "\t小e需要休息一小时!!!!!!"
#睡一个小时
time.sleep(1 * 60 * 60 )
except:
print "Exception"
exit( 0 )
print "----------------------------贵圈新闻数据流 check over------------------------------"
3.异常跟踪
Python 数据库连接的时候,报异常:ImportError: No module named release
Linux Fedora, CentOS系统:yum install MySQL-python 即可
4.mysql操作获取数量
#获取[昨天电影热度值符合规范]的数量
cursor.execute( "select count(*) from domain_movie_hot_records where record_date = ‘"+str(yesterday)+"‘ and news_num > 0" )
result = cursor.fetchone();
yesterday_movie_correct_hot_count = result[0]
python常用脚本以及问题跟踪