标签:
每三分钟爬一次,试了半月,一个邮件都没有,百度的产品,你懂得。代码测试是没有问题的
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 = ‘抢光了‘
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)
标签:
原文地址:http://my.oschina.net/u/2367514/blog/519541