说一下思路吧。以Shell版为例,我发现这个网页是静态的,也就是说直接抓取页面之后,分析一下关键字段,提取一些内容,就可以实现我说的功能。首先是当日的回帖数和发帖数,如果查看网页源代码,可以找到下面这行。
点击(此处)折叠或打开
- <span class="xs1 xw0 i">今日: <strong class="xi1">113</strong><span class="pipe">|</span>主题: <strong class="xi1">55427</strong>
点击(此处)折叠或打开
-
<em>[<a href="forum.php?mod=forumdisplay&fid=24&filter=typeid&typeid=506">文本处理</a>]</em> <a href="thread-4126289-1-1.html" onclick="atarget(this)" class="xst" >用vim将一列相同的数改成递加数</a>
-
<a href="forum.php?mod=redirect&tid=4126289&goto=lastpost#lastpost" class="xi1">New</a>
-
</th>
-
<td class="by" style="border:1px solid #000;width:114px;text-align:center" bgcolor="#F0F3FA">
-
<cite>
-
<a href="space-uid-26152505.html" c="1">done_and_todo</a></cite>
- <em><span class="xi1">2014-02-25</span></em>
接下来提一句,发送邮件本来在我自己的笔记本上直接用mail指令就可以做,考虑到自己的机器7*24小时运行负荷有点大,所以在linuxlearn.net上申请了一台虚拟主机,打算在上面跑,可惜它不提供mail指令,所以又用python做了个发送邮件的程序。
最后顺带提一句,在crontab里面运行的程序,最好用绝对路径否则在读写文件或者调用程序时可能出错,这点开始不注意,调了好久才发现的。
下面直接上代码。首先是用python抓去网页。
点击(此处)折叠或打开
-
#! /usr/bin/python
-
# -*- coding: UTF-8 -*-
-
-
import urllib2
-
-
try:
-
content=urllib2.urlopen('').read()
-
print content.decode('gbk').encode('utf-8')
-
# 编码按照utf8格式
-
except Exception,errorcode:
- print errorcode
点击(此处)折叠或打开
-
#! /usr/bin/python
-
# -*- coding: UTF-8 -*-
-
-
import smtplib,sys
-
from email.mime.text import MIMEText
-
-
mailto_list="nathanielwen@163.com"
-
# 设置目标邮箱列表
-
mail_host="smtp.163.com"
-
# 设置服务器
-
mail_user="automail2wen"
-
# 用户名
-
mail_pass="!@#123"
-
# 口令
-
mail_postfix="163.com"
-
# 发件箱的后缀
-
-
def send_mail(to_list,sub,content):
-
me="NoReply"+"<"+mail_user+"@"+mail_postfix+">"
-
msg = MIMEText(content,_subtype='html',_charset='utf8')
- # 因为发送的是html格式,所以这里的subtype要对应,编码方式也要对于
-
msg['Subject'] = sub
-
msg['From'] = me
-
msg['To'] = to_list
-
try:
-
server = smtplib.SMTP()
-
server.connect(mail_host)
-
server.login(mail_user,mail_pass)
-
server.sendmail(me, to_list, msg.as_string())
-
server.close()
-
return True
-
except Exception, e:
-
print str(e)
-
return False
-
-
if __name__ == '__main__':
-
subject="ChinaUnix论坛有更新"
-
content=sys.argv[1]
-
if send_mail(mailto_list,subject,content):
-
print "邮件发送成功"
-
else:
- print "邮件发送失败"
点击(此处)折叠或打开
-
#! /usr/bin/perl
-
-
$cwd='/home/nathanielwen/script/cu/';
-
$fpath="$cwd"."/.file";
-
-
chomp($now=`date '+%Y年%m月%d日 %H点%M分%S秒'`);
-
# 获得当前系统时间,并去掉换行符
-
-
open fh,"$fpath" || die "File open failed: $!\n";
-
chomp(@lines=<fh>);
-
close fh;
-
# .file文件格式如下:
-
# 第一行,时间
-
# 第二行,今日回帖数
-
# 第三行,总帖数
-
-
die "cfg read error.\n" unless(defined $lines[0] and defined $lines[1] and defined $lines[2]);
-
# 如果lines[0],lines[1]和lines[2]没有被赋值,说明读取.file文件时存在问题,终止程序
-
-
@webpage=`$cwd/getwebpage.py`;
-
# 运行python脚本将返回值保存在webpage中
-
-
for (@webpage){
-
if(/<span class="xs1 xw0 i">/){
-
/.*>(\d+)<.*>(\d+)<.*/;
-
if($2>$lines[2]){
-
$lines[2]=$2;
-
$lines[1]=$1;
-
$sendflag=1;
-
#如果有更新,则开启标志位
-
}
-
}
-
}
-
-
chomp($mdate=`date +%Y-%m-%d`);
-
$i=0;
-
for (@webpage){
-
if(/$mdate<\/span>/ and $webpage[$i-6] =~ m/\A<em>/){
-
$webpage[$i-6] =~ m/ >(.+)<\/a>$z/;
-
$title=$1;
-
$webpage[$i-5] =~ m/"(.+?)"/;
-
$href=""."$1";
-
$content.="
"."".">> $title"."";
-
}
-
$i++;
-
}
-
-
$mailstatus="无需发送邮件";
-
if($sendflag==1){
-
$sendflag=0;
-
$content =~ m/(.+)\Q$lines[3]/;
-
if(defined $1){
-
$mailstatus=`$cwd/sendmail.py "$1"`;
-
}else{
-
$mailstatus=`$cwd/sendmail.py "$content"`;
-
}
-
}
-
-
open fh,">$fpath" || die "File open failed: $!\n";
-
# 准备将从网站抓回来的参数回写到.file文件中
-
print fh "$now\n";
-
print fh "$lines[1]\n";
-
print fh "$lines[2]\n";
-
print fh "$content\n";
-
print fh "$mailstatus\n";
-
close fh;
- # 回写,并关闭文件句柄
这是手机端的邮件截图。绑定的是网易邮箱。如果有新帖子发出来的话会这样显示。