用户代码未处理 System.ArgumentNullException
HResult=-2147467261
Message=值不能为 null。
参数名: String Source=mscorlib
ParamName=String
StackTrace:
在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
在 NetOpinion.Form2.webBrowser1_DocumentCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e) 位置 d:\ProgrammingCode\NetOpinion1.2\NetOpinion\Form2.cs:行号 92
InnerException:
代码如下:
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.Text.RegularExpressions;
using System.Collections.Specialized;
namespace NetOpinion
{
public partial class Form2 : Form
{
int ipage,pagenum;
string text;
NameValueCollection nvc; //解析Url用
VR vr = new VR();
DataItem di = new DataItem();
SqlServer db = new SqlServer();
int start;
int final;
string httpbase = "http://www.tianya.cn/new/publicforum/articleslist.asp?pageno=1&stritem=";
public Form2()
{
ipage = 1;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (toolStripComboBox1.Text == "娱乐—娱乐八卦")
{
start = 9;
final = 108;
toolStripTextBox2.Text = httpbase + "funinfo&part=1";
}
else if (toolStripComboBox1.Text == "社会—天涯杂谈")
{
start = 9;
final = 108;
toolStripTextBox2.Text = httpbase + "free&part=1";
}
else if (toolStripComboBox1.Text == "社会—国际观察")
{
start = 9;
final = 53;
toolStripTextBox2.Text = httpbase + "worldlook&part=1";
}
else if (toolStripComboBox1.Text == "情感—情感天地")
{
start = 9;
final = 53;
toolStripTextBox2.Text = httpbase + "feeling&part=1";
}
else
{
MessageBox.Show("请选择论坛版面");
return;
}
if (toolStripTextBox1.Text == "")
{
MessageBox.Show("请输入抓取页数");
return;
}
if (toolStripComboBox2.Text == "选择时刻")
{
MessageBox.Show("请选择时刻");
return;
}
text = toolStripComboBox2.Text;
pagenum = int.Parse(toolStripTextBox1.Text);
webBrowser1.Navigate(toolStripTextBox2.Text, "_self");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.Document.Url != e.Url || e.Url.AbsoluteUri == "about:blank")
{
return;
}
ParseUrl(webBrowser1.Url.AbsoluteUri, out nvc);
if (nvc.Count != 0 && int.Parse(nvc.Get("pageNO")) > pagenum)
{
toolStripStatusLabel2.Text = "恭喜你!页面分析完毕,你可以查看结果了";
return;
}
toolStripStatusLabel2.Text = "当前正在抓取处理的页" + ipage.ToString() + "...";
ParsePage(webBrowser1.Document,start,final);
}
public void ParseUrl(string url, out NameValueCollection nvc) //解析网址,正则匹配
{
nvc = new NameValueCollection();
if (url == "")
{
return;
}
int questionMarkIndex = url.IndexOf('?');
if (questionMarkIndex == -1 || questionMarkIndex == url.Length - 1)
{
return;
}
string ps = url.Substring(questionMarkIndex + 1);
// 开始分析参数对
Regex re = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", RegexOptions.Compiled);
MatchCollection mc = re.Matches(ps);
foreach (Match m in mc)
{
nvc.Add(m.Result("$2"), m.Result("$3"));
}
}
public void ParsePage(HtmlDocument documentShare,int start,int final) //解析网页
{
int table = 1;
foreach (HtmlElement htmelt in documentShare.GetElementsByTagName("table"))
{
if ( table>=start && table<=final )
{
HtmlElement htmtr = htmelt.GetElementsByTagName("tr")[0];
GetTable(htmelt);
UpdateDataBase();
}
table++;
}
foreach (HtmlElement htmelt in documentShare.GetElementsByTagName("a"))
{
if (htmelt.InnerText == "下一页")
{
htmelt.InvokeMember("click");
ipage++;
}
}
}
}
}