当前位置:编程学习 > C#/ASP.NET >>

谁帮我分析一下以下代码:winform程序,抓网页的


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WebReader
{
    public partial class CommentListForm : Form
    {
        public CommentListForm()
        {
            InitializeComponent();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            CommentListReader reader = new CommentListReader();//这个不知什么意思
            reader.WordlookURL = txtURL.Text;
            CommentGrid.DataSource = reader.Read(); //这是从下面类读取来的??
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        public string CommentURL
        {
            get
            {
                bool selected = CommentGrid.SelectedRows != null && CommentGrid.SelectedRows.Count > 0;
                return selected ? CommentGrid.SelectedRows[0].Cells[0].Value.ToString() : string.Empty;
            }
        }
    }
}





using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace WebReader
{
    public class CommentListReader
    {
        public string WordlookURL;
        private string CommentPattern;
        private string TitlePattern;
        private string URLPattern;
        private string AuthorPattern;
        private string CreateTimePattern;
        private string ContentPattern;

        public CommentListReader()
        {
            CommentPattern = "<table width=640\\s*border=0\\s*cellspacing=0.*?>(?<comment>.*?)</table>";
            TitlePattern = "<td\\s*width=370>\\s*?<a\\s*href='.*?'\\s*target='_blank'>(?<title>.*?)</a>\\s*?</td>";
            URLPattern = "<td\\s*width=370>\\s*?<a\\s*href='(?<url>.*?)'\\s*target='_blank'>.*?</a>\\s*?</td>";
            AuthorPattern = "<td\\s*width=90>\\s*<a\\s*href='.*?'\\s*target=_blank>(?<author>.*?)</a></td>";
            CreateTimePattern = "<td\\s*width=90\\s*align=center\\s*class=tdfont>(?<createtime>.*?)</td>";
        }

        public CommentList Read()
        {
            string webhtml = HttpWebHelper.ReadWebHTML(WordlookURL);

            CommentList list = new CommentList();
            ReadComment(webhtml,list);
            return list;
        }

        private void ReadComment(string html, CommentList list)
        {
            Match match = Regex.Match(html, CommentPattern, RegexOptions.IgnoreCase);

            while (match.Success)
            {
                string commenthtml = match.Groups["comment"].Value;
                Comment model = new Comment();
                model.URL = ReadURL(commenthtml);
                model.Title = ReadTitle(commenthtml);
                model.Author = ReadAuthor(commenthtml);
                model.CreateTime = ReadCreateTime(commenthtml);
                list.Add(model);
                match = match.NextMatch();
            }
        }

        private string ReadTitle(string comment)
        {
            Match match = Regex.Match(comment, TitlePattern);
            string title = match.Groups["title"].Value;
            title = Regex.Replace(title, "<font color=.*?>", string.Empty);
            title = Regex.Replace(title, "</font>", string.Empty);
            return title;
        }

        private string ReadURL(string html)
        {
            Match match = Regex.Match(html, URLPattern);
            string url = match.Groups["url"].Value;
            return url;
        }

        private string ReadAuthor(string html)
        {
            Match match = Regex.Match(html, AuthorPattern);
            string author = match.Groups["author"].Value;
            return author;
        }

        private string ReadCreateTime(string html)
        {
            Match match = Regex.Match(html, CreateTimePattern);
            string createtime = match.Groups["createtime"].Value;
            return createtime;
        }
    }
}

--------------------编程问答-------------------- 1、上一个程序都引用了哪些类,哪些函数,什么作用? --------------------编程问答-------------------- CommentGrid《《这个不知道哪来的
主要功能就是CommentListReader实现的

CommentListForm 取得一个url 传递给CommentListReader实例,CommentListReader的Read()读取页面代码后用正则表达式处理
先匹配table,然后通过ReadComment取得其中的comment(内容),ReadTitle》Title(标题)……还有作者和创建时间这样与内容相关的东西
说白了就是 通过正则表达式 从table元素中提取文字内容

Read()最后将提取的内容返回给CommentGrid
--------------------编程问答-------------------- CommentGrid  是一个控件 即:DataGridview --------------------编程问答-------------------- (哐当)……

那么就是说,通过正则表达式获取网页内容后填充到DataGridview里去了……


CommentListReader reader = new CommentListReader();//将处理HTML的类实例化
reader.WordlookURL = txtURL.Text;//将地址传给reader
CommentGrid.DataSource = reader.Read(); //CommentGrid源 reader返回的CommentList类的list实例

还是比较清晰的……

CommentListReader类就是通过正则表达式获取网页内容,然后用list.Add方法添加到list里再返回…… --------------------编程问答-------------------- CommentListReader  的都是什么作用啊? --------------------编程问答-------------------- public CommentListReader()
      实例化类  定义正则
public CommentList Read()
       读取页面传入ReadComment处理并返回list
private void ReadComment(string html, CommentList list)
       通过正则表达式及调用下列各函数取得html的文字内容,并添加到list
private string ReadTitle(string comment)
      通过正则获取标题
private string ReadURL(string html)
        获取URl
private string ReadAuthor(string html)
       获取作者
private string ReadCreateTime(string html)
        获取创作时间


--|||一边打字我一边在冷汗……
不会这个看不懂吧……
:P --------------------编程问答--------------------

。。。。。

 private string ReadURL(string html)
        {
            Match match = Regex.Match(html, URLPattern);
            string url = match.Groups["url"].Value;
            return url;
        }




这个链接怎么表达能把链接的内容读出来啊???比如下面:能读:
标题:前苏联为什么要杀死2万多波兰被俘军官
作者:001130
URL:http://www.tianya.cn/publicforum/content/worldlook/1/206188.shtml  
时间:4-8 17:50

http://www.tianya.cn/publicforum/content/worldlook/1/206188.shtml内容没法取出来啊

[code=HTML]

<table width=640 border=0 cellspacing=0> 
<tr height=20><td width=20><font face=wingdings color=blue>K</font>    </td><td width=370><a href='http://www.tianya.cn/publicforum/content/worldlook/1/206188.shtml'  target='_blank'>前苏联为什么要杀死2万多波兰被俘军官</a></td> 
<td width=90><a href='/browse/Listwriter.asp?vwriter=001130' target=_blank>001130</a></td> 
    <td width=35 align=center class=tdfont>17540</td> 
    <td width=35 align=center class=tdfont>157</td> 
    <td width=90 align=center class=tdfont>4-8 17:50</td> 
</table> 
/code] --------------------编程问答--------------------

private void btnAccept_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;  //DialogResult这是干什么用的?
        }

--------------------编程问答-------------------- 他应该是自制的Dialog,在ShowDialog方法返回时,用以返回DialogResult的值
就是说
用这个类的时候是new然后ShowDialog,再判断返回值
而判断返回值需要DialogResult,所以用了btnAccept和btnClose来设置DialogResult

我个人理解是这样的 --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace WebReader
{
    public class CommentListReader
    {
        public string WordlookURL;
        private string CommentPattern;
        private string TitlePattern;
        private string URLPattern;
        private string AuthorPattern;
        private string CreateTimePattern;
        private string ContentPattern;

        public CommentListReader()
        {
            CommentPattern = "<table width=640\\s*border=0\\s*cellspacing=0.*?>(?<comment>.*?)</table>";
            TitlePattern = "<td\\s*width=370>\\s*?<a\\s*href='.*?'\\s*target='_blank'>(?<title>.*?)</a>\\s*?</td>";
            URLPattern = "<td\\s*width=370>\\s*?<a\\s*href='(?<url>.*?)'\\s*target='_blank'>.*?</a>\\s*?</td>";
            AuthorPattern = "<td\\s*width=90>\\s*<a\\s*href='.*?'\\s*target=_blank>(?<author>.*?)</a></td>";
            CreateTimePattern = "<td\\s*width=90\\s*align=center\\s*class=tdfont>(?<createtime>.*?)</td>";
        }

        public CommentList Read()
        {
            string webhtml = HttpWebHelper.ReadWebHTML(WordlookURL);

            CommentList list = new CommentList();
            ReadComment(webhtml,list);
            return list;
        }

        private void ReadComment(string html, CommentList list)
        {
            Match match = Regex.Match(html, CommentPattern, RegexOptions.IgnoreCase);

            while (match.Success)
            {
                string commenthtml = match.Groups["comment"].Value;
                Comment model = new Comment();
                model.URL = ReadURL(commenthtml);
                model.Title = ReadTitle(commenthtml);
                model.Author = ReadAuthor(commenthtml);
                model.CreateTime = ReadCreateTime(commenthtml);
                list.Add(model);
                match = match.NextMatch();
            }
        }

        private string ReadTitle(string comment)
        {
            Match match = Regex.Match(comment, TitlePattern);
            string title = match.Groups["title"].Value;
            title = Regex.Replace(title, "<font color=.*?>", string.Empty);
            title = Regex.Replace(title, "</font>", string.Empty);
            return title;
        }

        private string ReadURL(string html)
        {
            Match match = Regex.Match(html, URLPattern);
            string url = match.Groups["url"].Value;
            return url;
        }

        private string ReadAuthor(string html)
        {
            Match match = Regex.Match(html, AuthorPattern);
            string author = match.Groups["author"].Value;
            return author;
        }

        private string ReadCreateTime(string html)
        {
            Match match = Regex.Match(html, CreateTimePattern);
            string createtime = match.Groups["createtime"].Value;
            return createtime;
        }
    }
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,