采集新闻
2012-12-14 17:30
141 查看
面向对象的方式做采集程序
采集新闻
方便扩展
存储到xml
步骤:
1、找对象 文章视为对象 每一个网站视为对象
2、为了方便扩展做类似于计算器的操作
把采集的网站视为对象,所有的网站都能够采集 和保存成xml
所以抽象出父类WebSite 抽象类
实现具体的子类cnbeta sina等
3、WebSite 抽象类{ Name(网站名字 只读) Path xml保存路径 Url采集的url,抽象方法 Load采集新闻,Save把新闻保存到xml中}
4、cnbeta 继承WebSite{ }
donews
5、窗体加载时候根据反射读取每个继承自WebSite的子类的名字,添加到下拉框中
6、点采集按钮时候。根据下拉框中的内容创建具体的子类,执行采集方法
7、点保存按钮的时候 把采集到的新闻集合,存储在xml中
WebSite
Form1
采集新闻
方便扩展
存储到xml
步骤:
1、找对象 文章视为对象 每一个网站视为对象
2、为了方便扩展做类似于计算器的操作
把采集的网站视为对象,所有的网站都能够采集 和保存成xml
所以抽象出父类WebSite 抽象类
实现具体的子类cnbeta sina等
3、WebSite 抽象类{ Name(网站名字 只读) Path xml保存路径 Url采集的url,抽象方法 Load采集新闻,Save把新闻保存到xml中}
4、cnbeta 继承WebSite{ }
donews
5、窗体加载时候根据反射读取每个继承自WebSite的子类的名字,添加到下拉框中
6、点采集按钮时候。根据下拉框中的内容创建具体的子类,执行采集方法
7、点保存按钮的时候 把采集到的新闻集合,存储在xml中
WebSite
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 采集程序3 { public abstract class WebSite { public abstract string name { get; } private string url; public string Url { get { return url; } set { url = value; } } private string path; public string Path { get { return path; } set { path = value; } } public abstract List<Article> Load(); public abstract void Save(); } }
Form1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Reflection; namespace 采集程序3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //使用反射动态添加列表项,方便扩展 Assembly ass = sender.GetType().Assembly; Type[] types= ass.GetTypes(); foreach (Type type in types) { if (typeof(WebSite).IsAssignableFrom(type) && !type.IsAbstract) { WebSite ws = Activator.CreateInstance(type) as WebSite; comboBox1.Items.Add(ws.name); } } } WebSite ws; private void btnLoad_Click(object sender, EventArgs e) { ws = Fectory.CreateObj(comboBox1.Text); if (ws != null) { List<Article> articles = new List<Article>(); articles = ws.Load(); foreach (Article item in articles) { ListViewItem lvi = new ListViewItem(item.Title); lvi.SubItems.Add(item.Author); lvi.SubItems.Add(item.Content); lvi.SubItems.Add(item.Time.ToString()); listView1.Items.Add(lvi); } } else { MessageBox.Show("该选项不存在"); } } private void btnSave_Click(object sender, EventArgs e) { ws.Save(); MessageBox.Show("保存成功"); } private void listView1_DoubleClick(object sender, EventArgs e) { MessageBox.Show(listView1.SelectedItems[0].SubItems[0].Text); } } }
相关文章推荐
- 求助,怎么实现新闻自动采集并更新内容
- 新闻自动采集系统
- Python 网络爬虫(新闻采集脚本)
- 采集www.cnblogs.com 右侧边栏的最新新闻列表,以表格形式展示出来,无须入库。
- 正则匹配img标签 蜘蛛 爬取分析 新闻采集
- 自定义时间采集新浪的“新浪科技”栏目新闻(原型版)
- python - 采集 新浪新闻-国内-最新消息 转为xlsx
- Java基础系列6:计时器Timer与新闻的定时自动采集
- PHP扩展curl和正则表达式轻松采集新闻
- Java数据采集-6.获取开源中国新闻列表(翻页-2)
- 新闻文章信息采集功能的实现
- 新闻采集
- 数据采集(六):scrapy爬取搜狗微信新闻+selenium模拟鼠标点击
- 个性新闻自动采集系统
- 新闻采集应用程序
- 【开源】开发者新闻APP+新闻Restful服务+博客园新闻采集程序+infoq新闻采集程序+36kr新闻采集程序+oschina新闻采集程序+51cto新闻采集程序+csdn新闻采集程序
- php 采集新闻网站示例
- 用delphi制作新闻采集程序(一)
- 如何开发凤凰网资讯即时新闻采集爬虫
- 发布一款小软件:和讯博客助手-新闻采集测试版- 0.2.0