1. Time operation
Gets the current time format is%y-%m-%d%h:%m:%s
Currtime = Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ()))
Gets the day before date format is%y-%m-%d
Yesterday = Datetime.date.today ()-Datetime.timedelta (Days=1)
Get the first one-hour date
Yesterday = (Datetime.datetime.now ()-Datetime.timedelta (hours = 1)). Strftime ("%y-%m-%d%h:%m:%s")
2.mysql operation
#!/usr/bin/python
#coding =utf-8
Import MySQLdb
Import datetime
Import Mailutil
Import time
Try
Connection = MySQLdb.connect (user= "Entdb", passwd= "[email protected]", host= "192.168.32.47", db= "Miss8")
Except
Print "Could not connect to MySQL server."
Exit (0)
Print "----------------------------your lap news data stream check start------------------------------"
mailto_list=[' [email protected] ', ' [email protected] '
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 Current time:" + str (currtime) + "\ t two hours before the crawl quantity is:" +str (count)
if Count = = 0:
Tag = Mailutil.send_mail (Mailto_list, ' You have no data in your lap news data stream for two hours ', ' you have problems with your lap news stream, please check (47 database Gquan_article_info) to fix!!!!!! ')
If tag:
Print "Error\t Current time:" + str (currtime) + "\ t your lap news data flow problem \ t message sent successfully"
Else
Print "Error\t Current time:" + str (currtime) + "\ t your lap news data flow problem \ t message failed to send"
Else
Print "Info\t Current time:" + str (currtime) + "\ t your lap news data flow everything OK \ t no need to send mail"
Cursor.close ()
Print "Info\t Current time:" + str (currtime) + "\ t small e need to rest an hour!!!!!!"
#睡一个小时
Time.sleep (1 * 60 * 60)
Except
Print "Exception"
Exit (0)
Print "----------------------------your lap news data flow check over------------------------------"
3. Exception tracking
Python database connection, reported exception: Importerror:no module named release
Linux Fedora, CentOS system: Yum install Mysql-python
4.mysql Operation Get Quantity
The number of #获取 [yesterday's film's heat values conform to specification]
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 Common scripts and problem tracking