为了在朋友圈里多加些人,我就写了这个程序,将成员列表里的用户名提取出来,再写到dataview,然后填到textbox,程序自动点击“邀请”。用到了webbrowser,分析HTML,操作网页上的一些control,比如,点击,按钮。填入内容。

代码不多,一看就明白,就不多写了。

分析网页,取指定值,并存储,操纵网页,填值,模拟点击,容错,发送。_System

源码如下:

using System;
 using System.IO;
 using ;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;namespace WindowsApplication2
 { 
 
     public partial class 
     { 
 
         public Form1()
         { 
 
             InitializeComponent();
         }
         //根据Url地址得到网页的html源码
         private string GetWebContent(string Url)
         { 
 
             string strResult = "";
             try
             { 
 
                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                 //声明一个HttpWebRequest请求
                 request.Timeout = 30000;
                 //设置连接超时时间
                /*
 
 http://hexun.com/metababy http://ike.126.com 花纯春
 */
                 request.Headers.Set("Pragma", "no-cache");
                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                 Stream streamReceive = response.GetResponseStream();
                 Encoding encoding = Encoding.GetEncoding("GB2312");
                 StreamReader streamReader = new StreamReader(streamReceive, encoding);
                 strResult = streamReader.ReadToEnd();
             }
             catch
             { 
 
                 MessageBox.Show("出错");
             }
             return strResult;
         }        private void button1_Click(object sender, EventArgs e)
         { 
 
             webBrowser1.Navigate("http:///qiniuzhe/invite.aspx");
             //要抓取的URL地址
             string Url = textBox1.Text;            //得到指定Url的源码
             string strWebContent = GetWebContent(Url);
                        
             //取出和数据有关的那段源码
             int iBodyStart = strWebContent.IndexOf("<body", 0);
             int iStart1 = strWebContent.IndexOf("下页", iBodyStart);
             //int iTableStart1 = strWebContent.IndexOf("<table", iStart1);
             int iTableStart = strWebContent.IndexOf("<table", iStart1);
             //int iTableEnd1 = strWebContent.IndexOf("</table>", iTableStart);
             int iTableEnd = strWebContent.IndexOf("</table>", iTableStart);
             string strWeb = strWebContent.Substring(iTableStart, iTableEnd - iTableStart + 8);            
           
             
             //生成HtmlDocument
             WebBrowser webb = new WebBrowser();
             webb.Navigate("about:blank");
             HtmlDocument htmldoc = webb.Document.OpenNew(true);
             
             htmldoc.Write(strWeb);
           
             
                         
             
                         
             
             HtmlElementCollection htmlTD = htmldoc.GetElementsByTagName("TD");
             foreach (HtmlElement td in htmlTD)
              { 
 
                  string strID = td.InnerText;
                  int IDend = strID.Length;
                  int IDstart = strID.LastIndexOf("200");
                  int ij = IDstart-1;
                  string abc = strID.Substring(1, ij);                // string strName = tr.GetElementsByTagName("TD")[1].InnerText;
                  //string strSinger = tr.GetElementsByTagName("TD")[1].InnerText;
                  //strID = strID.Replace(".", "");
                  //插入DataTable
                  /*
 
 http://hexun.com/metababy http://ike.126.com 花纯春
 */              DataGridViewRowCollection drow = this.dataGridView1.Rows;   
               drow.Add(abc);
               
               
                 
                   
                   
                 
                                 
              }
         
         }
         int iii = -1;        private void button2_Click(object sender, EventArgs e)
         { 
 
             webBrowser1.Navigate("http:///qiniuzhe/invite.aspx");            iii = iii + 1;
             webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(test);
             
             }
                 private void test(object sender, WebBrowserDocumentCompletedEventArgs e)
         { 
              HtmlElementCollection webUser = webBrowser1.Document.All.GetElementsByName("User");
            if (iii < Convert.ToInt32(dataGridView1.Rows.Count.ToString()))
             { 
 
                 
                 this.dataGridView1.CurrentCell = this.dataGridView1[0,iii];                
                 webUser[0].InnerText = dataGridView1.CurrentCell.Value.ToString().Trim();                 HtmlElementCollection webSub = webBrowser1.Document.All.GetElementsByName("submitInvite");
                 webSub[0].InvokeMember("click");
                 
             }
             else 
             { 
 
                 MessageBox.Show("弄完了!");
             }
 /*
 
 http://hexun.com/metababy http://ike.126.com
 */         }
       
    }
    }