Skip to content

Instantly share code, notes, and snippets.

@forthxu
Last active March 29, 2016 06:34
Show Gist options
  • Select an option

  • Save forthxu/0b9e2d23912a1918c7e8 to your computer and use it in GitHub Desktop.

Select an option

Save forthxu/0b9e2d23912a1918c7e8 to your computer and use it in GitHub Desktop.
业务需要,抓取页面排名数据保存,月末发邮件
<?php
foreach (glob("./data/*.html") as $filename) {
echo "<a href='$filename'>$filename</a><br/>";
}
?>
#!/usr/bin/python2.7
# -*- coding: gbk -*-
from bs4 import BeautifulSoup
import urllib2
import smtplib
from email.mime.text import MIMEText
import time
import calendar as cal
import sys,os
def cur_file_dir():
path = sys.path[0]
if os.path.isdir(path):
return path
elif os.path.isfile(path):
return os.path.dirname(path)
if __name__ == "__main__":
#f = open('index.html','rb')
#s = f.read()
#f.close()
#u = s.decode('gbk')
path = cur_file_dir()+'/data'
if not os.path.exists(path):
os.makedirs(path)
url = 'http://huodong2.4399.com/girl/girlblg/'
t = time.strftime("%Y-%m-%d %H:%M:%S")
d = time.strftime("%Y-%m-%d")
l = cal.monthrange(int(time.strftime("%Y")), int(time.strftime("%-m")))
u = urllib2.urlopen(url)
soup = BeautifulSoup(u, "html5lib")
title = soup.title.string.encode('utf-8')
data = '<table>'
li_a = soup.find_all("a", class_="side5-tit")
li_fen = soup.find_all("span", class_="side5-fen")
for i in range(0, len(li_a)):
data=data+'<tr><td>'+str(i+1).encode('utf-8')+'</td><td>'+li_fen[i].string.encode('utf-8')+'</td><td>'+li_a[i]['href'].encode('utf-8')+'</td><td>'+li_a[i].string.encode('utf-8')+"</td></tr>"
data+='</table>'
host = "smtp.exmail.qq.com"
port = 465
user = "x@4399inc.com"
pwd = "password"
sender=user
receiver = 'y@4399inc.com'
cc = 'z@4399inc.com,z@4399inc.com'
bcc = 's@qq.com'
body = "<h1>"+title+" rank</h1>"
body += data
body += "<p>time "+t+" from "+sender+".</p>"
body += "<p>other day data <a href='http://t.huodong.4399.com/girl/2016girlblg/data.php'>http://t.huodong.4399.com/girl/2016girlblg/data.php</a>.</p>"
f=file(path+"/"+t+".html","w+")
f.write(body)
f.close()
if l[1]==int(time.strftime("%-d")):
msg = MIMEText(body, "html")
msg["subject"] = ''+title+" rank "+d
msg["from"] = sender
msg["to"] = receiver
msg['Cc'] = cc
receivers = cc.split(",") + bcc.split(",") + [receiver]
s = smtplib.SMTP_SSL(host, port);
#s.set_debuglevel(1)
s.login(user, pwd)
s.sendmail(sender, receivers, msg.as_string())
#!/usr/bin/sh
#this script is used to start the xxx in the last day of every month
ym=`date +%m" "%Y`
if [ `date +%d` = `cal $ym|xargs|awk '{print $NF}'` ]
then
#/usr/local/python-2.7.11/bin/python2.7 /workspace/work/girlblg/work.py
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment