python 利用爬蟲監控百度免費試用網頁是否有使用機會

每三分鐘爬一次,試了半月,一個郵件都沒有,百度的產品,你懂得。代碼測試是沒有問題的html

import requestsjson

import jsonapp

import loggingdom

import repost

import datetime測試

import time網站

import smtplib  url

from email.mime.text import MIMETextspa

import randomcode


#URL='http://120.25.146.222:8002/'

URL ='http://bce.baidu.com/event/worldConference_event.html?qq-pf-to=pcqq.group'

KEY = '搶光了'


mail_host = "smtp.exmail.qq.com" 

mail_user = "*******"

mail_pwd = "******"

to_list = ["*******","******"]

mail_postfix = '內褲穿四年'

subject = "%s 網頁已經準備好" % datetime.datetime.now().strftime("%Y-%m-%d")

content = "網站 {0} 已經不含:{1},請速度去搶機會".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}次監控'.format(i))

        if not m:

            try:

                print('能夠搶了,已經將發送郵件')

                send_mail(to_list,subject,content)

                

            except Exception as e:

                logging.error(str(e))

                print(e)

            time.sleep(120) # 若是發送過郵件等2分支後在發送

        else:

            print('還不能搶')

        

        time.sleep(imon_time)

        i+=1

            


if __name__ == '__main__':

    log_init()

    tag(URL,KEY)

相關文章
相關標籤/搜索