Every three minutes to climb once, try for half a month, an email is not, Baidu products, you know. Code testing is not a problem
Import requests
Import JSON
Import logging
Import re
Import datetime
Import time
Import Smtplib
From Email.mime.text import Mimetext
Import Random
#URL = ' http://120.25.146.222:8002/'
URL = ' Http://bce.baidu.com/event/worldConference_event.html?qq-pf-to=pcqq.group '
KEY = ' robbed '
Mail_host = "Smtp.exmail.qq.com"
Mail_user = "*******"
MAIL_PWD = "******"
To_list = ["*******", "******"]
Mail_postfix = ' underwear worn for four years '
Subject = "%s Web page is ready"% datetime.datetime.now (). Strftime ("%y-%m-%d")
Content = "Site {0} already does not contain: {1}, please speed to seize the opportunity". Format (Url,key)
Log_path= "/root/my_app/imon.log"
Imon_time = Random.randint (30,60)
Def log_init (level=logging. DEBUG,
format= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ',
Datefmt= '%a,%d%b%Y%h:%m:%s ',
Filename=log_path,
Filemode= ' W '):
Logging.info (' This is Info message ')
Logging.warning (' This is warning message ')
def send_mail (to_list,subject,content):
Me= "Hello" + "<" +mail_user+ "@" +mail_postfix+ ">"
msg = Mimetext (content,_subtype= ' plain ', _charset= ' utf-8 ')
msg[' Subject '] = Subject
Msg[' from '] = Me
Msg[' to '] = ";". Join (To_list)
Try
Server = Smtplib. SMTP ()
Server.connect (Mail_host)
Server.login (MAIL_USER,MAIL_PWD)
Server.sendmail (Me, To_list, msg.as_string ())
Server.close ()
Return True
Except Exception as E:
Print (str (e))
Return False
def tag (Url,key):
I=1
While 1:
Try
r = Requests.get (URL)
Cont =r._content.decode (' Utf-8 ')
Except Exception as E:
Logging.error (str (e))
Print (e)
m = Re.search (Key,cont)
Print (' {0} secondary monitoring '. Format (i))
If not m:
Try
Print (' Can Rob, already send Mail ')
Send_mail (to_list,subject,content)
Except Exception as E:
Logging.error (str (e))
Print (e)
Time.sleep (120) # If you send a message such as 2 branches after sending
Else
Print (' Not yet robbed ')
Time.sleep (Imon_time)
I+=1
if __name__ = = ' __main__ ':
Log_init ()
Tag (Url,key)
Python uses crawler to monitor Baidu free trial Web site If there is a chance to use