// 这里URL=http://www.cz88.net/proxy/index.aspx
      public static string GetHtml(string URL)
        {
            WebRequest wr;
            wr = WebRequest.Create(URL);
            wr.Credentials = CredentialCache.DefaultCredentials;
            WebResponse wp;
            wp = wr.GetResponse();
   
            string html = new StreamReader(wp.GetResponseStream(), Encoding.UTF8).ReadToEnd();
            return html;
        }  
获取了HTML  就是网页源码  现在的问题就是:我想截取上面的 代理IP和端口
格式就是 xxx.xxx.xxx.xxx:8080
         xxx.xxx.xxx.xxx:8080
很多条 然后写入TXT文本     

解决方案 »

  1.   

    怎么获取多条 并写入TXT呢
      

  2.   

    foreach(Match m in Regex.Mathes(html,@"\d+(\.\d+){3}:\d+"))
    {
      //m.Value就是你想要的
    }
      

  3.   

    foreach (Match m in reg.Matches(html, @"\d+(\.\d+){3}:\d+")){
    Ctxt(m.Value);
    }
    是这样吧
    报错。 无法使用实例来访问成员
    我上面的代码是
     Regex reg = new Regex(@"\d+(\.\d+){3}:\d+");
                string html=GetHtml("http://www.cz88.net/proxy/index.aspx");
      

  4.   

            private void button1_Click(object sender, EventArgs e)
            {
                Regex reg;
                string html=GetHtml("http://www.cz88.net/proxy/index.aspx");
                foreach (Match m in reg.Matches(html, @"\d+(\.\d+){3}:\d+"))
                {
                    Ctxt(m.Value);
                }
            }
            /// <summary>
            /// 创建文本并写入内容
            /// </summary>
            /// <param name="content"></param>
            public void Ctxt(string content)
            {
                string file = "tt.txt";
                //string content = txtContent.Text;//内容
                if (!File.Exists(file) == true)
                {
                    MessageBox.Show("存在此文件!");
                }
                else
                {
                    FileStream myFs = new FileStream(file, FileMode.Create);
                    StreamWriter mySw = new StreamWriter(myFs);
                    mySw.Write(content);
                    mySw.Close();
                    myFs.Close();
                    MessageBox.Show("写入成功");
                }
            }        /// <summary>
            /// 采集函数
            /// </summary>
            /// <param name="URL">地址</param>
            /// <returns>返回采集到的HTML</returns>
            public string GetHtml(string URL)
            {
                WebRequest wr;
                wr = WebRequest.Create(URL);
                wr.Credentials = CredentialCache.DefaultCredentials;
                WebResponse wp;
                wp = wr.GetResponse();
       
                string html = new StreamReader(wp.GetResponseStream(), Encoding.UTF8).ReadToEnd();            return html;
            }
      

  5.   


            private void button1_Click(object sender, EventArgs e)
            {
                Regex reg;
                string html=GetHtml("http://www.cz88.net/proxy/index.aspx");
                foreach (Match m in reg.Matches(html, @"\d+(\.\d+){3}:\d+"))
    //这里报错
    请改用类型名来限定它            {
                    Ctxt(m.Value);
                }
            }
            /// <summary>
            /// 创建文本并写入内容
            /// </summary>
            /// <param name="content"></param>
            public void Ctxt(string content)
            {
                string file = "tt.txt";
                //string content = txtContent.Text;//内容
                if (!File.Exists(file) == true)
                {
                    MessageBox.Show("存在此文件!");
                }
                else
                {
                    FileStream myFs = new FileStream(file, FileMode.Create);
                    StreamWriter mySw = new StreamWriter(myFs);
                    mySw.Write(content);
                    mySw.Close();
                    myFs.Close();
                    MessageBox.Show("写入成功");
                }
            }        /// <summary>
            /// 采集函数
            /// </summary>
            /// <param name="URL">地址</param>
            /// <returns>返回采集到的HTML</returns>
            public string GetHtml(string URL)
            {
                WebRequest wr;
                wr = WebRequest.Create(URL);
                wr.Credentials = CredentialCache.DefaultCredentials;
                WebResponse wp;
                wp = wr.GetResponse();
       
                string html = new StreamReader(wp.GetResponseStream(), Encoding.UTF8).ReadToEnd();            return html;
            }
      

  6.   


    你只写入一次,然后 mySw.Close();
      myFs.Close();
    就关闭了,你怎么还能写入呢。
    foreach是循环啊。
      

  7.   

    foreach (Match m in reg.Matches(html, @"\d+(\.\d+){3}:\d+"))
    ========
    foreach (Match m in Regex.Matches(html, @"\d+(\.\d+){3}:\d+"))
      

  8.   

    嗯  这个改对了  但是m.Value的值还是是空的。 
      

  9.   

    是你问的不对,HTML源码里IP和端口是分开的: <tr><td>94.112.3.71</td><td>1080</td><td>透明</td><td>whois</td><td><div class="addr_style">捷克  CZ88.NET</div></td></tr>根本不是你所说的那样:格式就是 xxx.xxx.xxx.xxx:8080
      xxx.xxx.xxx.xxx:8080
    private void button1_Click(object sender, EventArgs e)
      {
      string html=GetHtml("http://www.cz88.net/proxy/index.aspx");
    foreach (Match m in Regex.Matches(html, @"(\d+(\.\d+){3})\D+(\d+)"))
    //这里报错请改用类型名来限定它 {
      Ctxt(m.Groups[1].Value+":"+m.Groups[3].Value);
      }
      }