http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3310|回复: 5

[其他] 正则匹配出的内容,如何用timer控制每隔一段时间显示一条?

[复制链接]
发表于 2013-6-15 02:27:19 | 显示全部楼层 |阅读模式
本帖最后由 Monn 于 2013-6-15 02:28 编辑

用正则取出了网页源码中的url和标题,怎么用timer控制,每隔一秒钟显示一条啊。。
我把正则的整个for循环放入timer的事件里,只会每隔一秒读取N条,而不是一条,
只把for里边的东西放到timer事件里,每隔一秒显示的是同一条信息。。
大家帮忙看看哈。。我要的效果是每隔一秒显示一条。。


[code=csharp]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DotNet.Utilities;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        HttpHelper http = new HttpHelper();
        HttpItem item = null;//创建HTTP访问类对象
        string html="";
        string Url = "";
        string Title = "";
        private string gethtml(string url)
        {
            item = new HttpItem()
            {
                URL = url,//URL     必需项
                Method = "get",//URL     可选项 默认为Get
                Cookie = ""//当前登录Cookie
            };                    
            HttpResult result = http.GetHtml(item);
            return html = result.Html;
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
           
            gethtml("http://news.sohu.com/1/0903/61/subject212846158.shtml");
            Regex reg = new Regex(@"·<a href=(?<url>([^\""""\'>\s]*)) target=_blank>(?<title>([^<]+|.*?)?)</a><span>");
            MatchCollection mc = reg.Matches(html);
            foreach (Match m in mc)
            {
                Url = m.Groups["url"].Value;
                Title = m.Groups["title"].Value;
                timer1.Interval = 1000;
                timer1.Start(); //启动timer1
            }         
            
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox1.AppendText("标题为:" + Title + "\r\n网址为:" + Url + "\r\n\r\n");
        }
    }
}

[/code]


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-6-15 11:01:10 | 显示全部楼层
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using DotNet.Utilities;
  9. using System.Text.RegularExpressions;

  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form2 : Form
  13.     {
  14.         public Form2()
  15.         {
  16.             InitializeComponent();
  17.         }

  18.         HttpHelper http = new HttpHelper();
  19.         HttpItem item = null;//创建HTTP访问类对象
  20.         string html = "";
  21.         string Url = "";
  22.         string Title = "";
  23.         private string gethtml(string url)
  24.         {
  25.             item = new HttpItem()
  26.             {
  27.                 URL = url,//URL     必需项
  28.                 Method = "get",//URL     可选项 默认为Get
  29.                 Cookie = ""//当前登录Cookie
  30.             };
  31.             HttpResult result = http.GetHtml(item);
  32.             return html = result.Html;
  33.         }
  34.         MatchCollection mc = null;
  35.         int i = 0;
  36.         private void button1_Click(object sender, EventArgs e)
  37.         {
  38.             gethtml("http://news.sohu.com/1/0903/61/subject212846158.shtml");
  39.             Regex reg = new Regex(@"·<a href=(?<url>([^""""\'>\s]*)) target=_blank>(?<title>([^<]+|.*?)?)</a><span>");
  40.             mc = reg.Matches(html);
  41.             timer1.Enabled = true;
  42.         }
  43.         private void timer1_Tick(object sender, EventArgs e)
  44.         {
  45.             if (i < mc.Count)
  46.             {
  47.                 Match m = mc[i];
  48.                 i++;
  49.                 Url = m.Groups["url"].Value;
  50.                 Title = m.Groups["title"].Value;
  51.                 textBox1.AppendText("标题为:" + Title + "\r\n网址为:" + Url + "\r\n\r\n");
  52.             }
  53.             else
  54.             {
  55.                 textBox1.AppendText("读完了\r\n\r\n");
  56.                 timer1.Enabled = false;
  57.             }

  58.         }
  59.     }
  60. }
复制代码
源码下载
WindowsFormsApplication1.zip (69.52 KB, 下载次数: 183)
发表于 2013-6-15 11:05:35 | 显示全部楼层
我只是路过打酱油的。
发表于 2013-6-15 11:17:46 | 显示全部楼层
[code=csharp]public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }
        System.Threading.Thread t;
        private delegate void VoidDelegate();
        private void MainFrm_Load(object sender, EventArgs e)
        {
            t = new System.Threading.Thread(new System.Threading.ThreadStart(this.gethtml));
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (t.ThreadState == System.Threading.ThreadState.Unstarted)
            {
                t.Start();
                this.button1.Text = "暂停";
            }
            else
            {
                if (t.ThreadState == System.Threading.ThreadState.Suspended)
                {
                    t.Resume();
                    this.button1.Text = "暂停";
                }
                else
                {
                    t.Suspend();
                    this.button1.Text = "继续";
                }
            }
        }
        private void MainFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (t.ThreadState == System.Threading.ThreadState.Suspended)
            {
                t.Resume();
                t.Abort();
            }
        }





        private void gethtml()
        {
            HttpHelper http = new HttpHelper();
            HttpItem item = null;//创建HTTP访问类对象

            item = new HttpItem()
            {
                URL = "http://news.sohu.com/1/0903/61/subject212846158.shtml",//URL     必需项
                Method = "get",//URL     可选项 默认为Get
                Cookie = ""//当前登录Cookie
            };                    
            HttpResult result = http.GetHtml(item);
            Regex reg = new Regex(@"·<a href=(?<url>([^\""""\'>\s]*)) target=_blank>(?<title>([^<]+|.*?)?)</a><span>");
            MatchCollection mc = reg.Matches(result.Html);
            foreach (Match m in mc)
            {
                string url = m.Groups["url"].Value;
                string title = m.Groups["title"].Value;
                this.BeginInvoke(new VoidDelegate(delegate(){
                    textBox1.AppendText("标题为:" + title + "\r\n网址为:" + url + "\r\n\r\n");
                }));
               System.Threading.Thread.Sleep(1000);
            }
        }
    }[/code]

多线程版
 楼主| 发表于 2013-6-15 11:56:39 | 显示全部楼层

多谢老大,问题已解决。。。
 楼主| 发表于 2013-6-15 11:57:17 | 显示全部楼层

多谢大哥提供多方法解决方案。。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-5 18:30

© 2014-2021

快速回复 返回顶部 返回列表