网络爬虫,这个概念其实我比较模糊。网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁,自动索引,模拟程序或者蠕虫。
这两天写了一个小的程序吧!检查大家这周是否写博客了,并且判断上周是否写博客了(没有写就发邮件),(周六判断 )如果上周没有写说明违纪了。把相对应的记录写的一个文本文档中。这个小程序,方便提醒咱们,并且方便博委查询。
通过DownloadString这个方法可以把网页的源码下载下来,这样把博客的每篇时间取出来就可以了,但是有一种新的情况就是如果置顶怎么办?
这样可以通过正则把置顶两个字取出来,查看有几篇置顶的博客。来选取首篇博客进行判断(把置顶的首篇和不置顶的博客首篇都拿到查询)。
汉字的正则怎么匹配?
MatchCollection lista = Regex.Matches(html, @regex);
if (lista.Count > 0)
来让大家看一下源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
//
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//1.程序循环每13小时执行一次程序
//System.Timers.Timer timer = new System.Timers.Timer();
//timer.Enabled = true;
//timer.Interval = 60000;//执行间隔时间,单位为毫秒
//timer.Start();
//timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer1_Elapsed);
//timer.AutoReset = true; //一分钟一次,保证每一分钟都执行一次。
// private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
//{
// 里面是需要循环的代码}
//2.判断当前是星期六如果是进行程序加载
DateTime dt = DateTime.Now; //得到当前的日期
if ((int)dt.DayOfWeek == 6)
{
//进行excel表格的查询判断每个人是否在本周的星期六之前写了博客
blogmail(Convert.ToDateTime(dt.ToString("yyyy-MM-dd")));
}
Console.ReadKey();
}
public static void blogmail(DateTime dt) {
//3进行excel博客的加载
//#
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=博客地址.xlsx;Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'"; OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
//4得到第一个人的博客目录,查询到了它的首篇博客
OleDbDataAdapter myCommand = null;
DataTable table = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
table = new DataTable();
myCommand.Fill(table);
for (int tabletemp = 0; tabletemp < table.Rows.Count; tabletemp++)
{
tgbperson bolgperson = new tgbperson();
bolgperson.name = table.Rows[tabletemp][0].ToString();
bolgperson.periods = table.Rows[tabletemp][1].ToString();
bolgperson.csdnaddress = table.Rows[tabletemp][2].ToString();
bolgperson.mailaddress = table.Rows[tabletemp][3].ToString();
testsumday(bolgperson,dt);
}
}
//判断天数,逐个方法的判断看是否违纪,需要发邮件
public static void testsumday(tgbperson mary,DateTime dt)
{
//5进行判断
WebClient wb = new WebClient();
//string html = wb.DownloadString(@"http://blog.csdn.net/liuziteng0228?viewmode=contents");
wb.Encoding = System.Text.Encoding.GetEncoding("utf-8");
string html = wb.DownloadString(@""+mary.csdnaddress+"");
//正则表达式提取日期--这里会有置顶的文章
Regex reg = new Regex(@"\d{4}-\d{2}-\d{2}");
Match hzk= reg.Match(html);
//正则表达式提取汉字
//定义变量用于包含符合的那个时间,去掉置顶的时间
string removezd = hzk.ToString();
string regex = "置顶";
MatchCollection lista = Regex.Matches(html, @regex);
if (lista.Count > 0)
{
MatchCollection hzkarry= reg.Matches(html);
int listazons = lista.Count + 1;
foreach (var item in hzkarry)
{
listazons--;
if (listazons==0)
{
//如果有置顶的这样判断,看置顶的和正常的第一个相比较
if (DateTime.Compare(Convert.ToDateTime(hzk.ToString()), Convert.ToDateTime(item.ToString()))>0)
{
Console.WriteLine(mary.name+hzk+"!!!");
removezd = hzk.ToString();
}
else
{
Console.WriteLine(mary.name + item.ToString()+"!!!");
removezd = item.ToString();
}
}
}
}
//进行日期转换得到日期格式,dateblogrealy博客首篇的时间
DateTime dateblogrealy = Convert.ToDateTime(removezd);
int i = 0;
bool flag = true;
while (flag)
{
if (dateblogrealy.AddDays(i) == dt)
{
//i表示和当前时间的天数之差
if (i >= 6 && i < 13)
{
//从这里开始邮件的发送提醒
flag = false; //中断while
sendmail smail = new sendmail();
string mailTo="15930649145@163.com";
string mailSubject="博客没有写";
string mailContent = mary.name +"ta没有写";
smail.SendEmail(mailTo,mailSubject,mailContent);
Console.WriteLine(mary.name+ "发送了");
}
if (i >= 13)
{
flag = false; //中断while
//从这里开始插入数据库
write(mary, dt);
Console.WriteLine(mary.name+ "违纪了吧!");
break;
}
if (i<6)
{
Console.WriteLine(mary.name+"没有问题啦!");
flag = false;
}
}
//如果等于13已经违纪了,不再等待相同了
if (i >= 13)
{
//之间进行数据的插入,违纪了
write(mary, dt);
Console.WriteLine(mary.name + "违纪了吧!");
flag = false;
}
i++;
}
}
//错误了,把信息打到文本文档中
public static void write(tgbperson mary,DateTime dt) {
StreamWriter write = null;
string time = dt.ToString();
string LogName = "log.logg";
string logPath = "Debug//Error";
string log = logPath + LogName;
if (!File.Exists(log))
{
//创建文件夹
Directory.CreateDirectory(logPath);
//创建文本文档
File.CreateText(log);
}
else
{
write = File.AppendText(log);
write.WriteLine("时间" + time+"上周博客没有写");
write.WriteLine("期数"+mary.periods);
write.WriteLine("姓名"+mary.name);
write.WriteLine("-----------------");
write.Flush();
write.Dispose();
}
}
//发送邮件
public static void sendmail(tgbperson mary, DateTime dt) {
}
}
class ok{
public void circulation() {
System.Timers.Timer timer = new System.Timers.Timer();
timer.Enabled = true;
timer.Interval = 1000;//执行间隔时间,单位为毫秒
timer.Start();
timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer1_Elapsed);
timer.AutoReset = true; //一分钟一次,保证每一分钟都执行一次。
}
private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
}
}
}
class tgbperson
{
public string name { get;set;}
public string periods { get; set; }
public string csdnaddress { get; set; }
public string mailaddress { get; set; }
}
发送邮件的代码
public bool SendEmail(string mailTo, string mailSubject, string mailContent)
{
// 设置发送方的邮件信息,例如使用网易的smtp
string smtpServer = "smtp.163.com"; //SMTP服务器
string mailFrom = "15930649145@163.com"; //登陆用户名
string userPassword = "xiao123";//登陆密码
// 邮件服务设置
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
smtpClient.Host = smtpServer; //指定SMTP服务器
smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码
// 发送邮件设置
MailMessage mailMessage = new MailMessage(mailFrom, mailTo); // 发送人和收件人
mailMessage.Subject = mailSubject;//主题
mailMessage.Body = mailContent;//内容
mailMessage.BodyEncoding = Encoding.UTF8;//正文编码
mailMessage.IsBodyHtml = true;//设置为HTML格式
mailMessage.Priority = MailPriority.Low;//优先级
smtpClient.Send(mailMessage); // 发送邮件
try
{
return true;
}
catch (SmtpException ex)
{
return false;
}
}