最近在起名网测名字,名字最后一个字是十二画的,笔画十二画的汉字有将近2000,人工测试用时粗略估计是(如果是想测试网站的性能的话,可以试试,具体操作时间有兴趣的可以总结下),废话有点多,转入主题------利用WebBrowser加载自动生成的页面,再用StreamReader读取页面内容,最后抓取网页中的指定字符串(及姓名测试分值),具体如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{ this.Start();}
private void Start()
{
string NewName = "�a�j���,W�掌�龛��t�凑菰�����z�Z�L���`�F��\�]�o���@筝�^��彘骘��……"; //字太多就不都列出来了
List<Charactor> list = new List<Charactor>();
for (int i = 0; i < Character.Length; i++)
{
NewName = Character.Substring(i, 1);
list.Add(new Charactor(NewName));
}
GetHtml(list);
DRCNet.Utility.LogUtility.WriteLog("Game Over", "NameScore.txt");
}
bool loading = true; //该变量表示网页是否正在加载.
string html = string.Empty;
WebBrowser browser = new WebBrowser();
public void GetHtml(List<Charactor> names)
{
browser.Navigated += new WebBrowserNavigatedEventHandler(browser_Navigated);
foreach (Charactor name in names)
{
loading = true; //表示正在加载
browser.Navigate("http://www.1518.com/s?st=2&word1=逍遥&word2=" + name.Ming); // 加载页面,传入参数,word1姓氏,word2名称
this.textBox1.Text = name.Ming;
while (loading)
{
Application.DoEvents();//等待本次加载完毕才执行下次循环.
}
}
}
int i = 0;
private void browser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
i++;
if (i % 10 == 0) // 假设每张页面要执行3次browser_Navigated方法,那么这表示网页全部内容加载完成.(至于这个3要怎么样得到,那是仁者见仁的事情了,呵呵)
{
loading = false;//在加载完成后,将该变量置为false,下一次循环随即开始执行.
string _name = this.textBox1.Text;//.Substring(this.textBox1.Text.Length - 2);
System.IO.StreamReader getReader = new System.IO.StreamReader(browser.DocumentStream, System.Text.Encoding.GetEncoding("gb2312"));
string html = getReader.ReadToEnd();
int _index = html.IndexOf("<div class=\"njfen\">");
string _html = html.Remove(0, _index + 19);
string _score = html.Length > 0 ? _html.Substring(0, 2) : "0";
DRCNet.Utility.LogUtility.WriteLog(_name + " " + _score, "NameScore.txt");
i = 0;
}
}
}
public class Charactor
{
private string _ming;
public string Ming
{
get { return this._ming; }
set { this._ming = value; }
}
public Charactor() { }
public Charactor(string name)
{
this._ming = name;
}
}
不足之处,望帮忙指正! 祝大家中秋快乐!