Python uses crawler to monitor Baidu free trial Web site If there is a chance to use

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.