2 longmansing longmansing 于 2014.05.12 22:31 提问

用户代码未处理 System.ArgumentNullException

用户代码未处理 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++;
            }
        }
    }
}

}

2个回答

feiyun0112
feiyun0112   Rxr 2014.05.13 09:08

调试看看
nvc.Get("pageNO")的值不对

longmansing
longmansing 不知道啊,改错了吧。那个nvc.Get("pageNO")不是这样改的。
大约 4 年之前 回复
feiyun0112
feiyun0112 那就跟踪看代码怎么走的,为什么没执行
大约 4 年之前 回复
longmansing
longmansing 没执行啊。那样改不可以的吧
大约 4 年之前 回复
longmansing
longmansing 呵呵,我找不到pageNO这个值啊,不知道是什么意思?我把nvc.Get("pageNO")换为ipage后可以正常运行,不过无法把抓取的数据读到数据库中
大约 4 年之前 回复
longmansing
longmansing   2014.05.14 17:13

public void GetTable(HtmlElement htmelt)
{
int item = 0;
foreach (HtmlElement htmtd in htmelt.GetElementsByTagName("td"))
{
if (item == 1)
{
HtmlElement htmtltle = htmtd.GetElementsByTagName("a")[0];
di.bbstitle = htmtltle.InnerText.Replace("\"", " ").Replace("'", " ");
di.bbsurl = htmtltle.GetAttribute("href");
}
else if (item == 2)
{
HtmlElement htmauthor = htmtd.GetElementsByTagName("a")[0];
di.bbsauthor = htmauthor.InnerText;
}
else if (item == 3)
{
vr.bbsview = int.Parse(htmtd.InnerText);
}
else if (item == 4)
{
vr.bbsreply = int.Parse(htmtd.InnerText);
}
else if (item == 5)
{
di.submittime = htmtd.InnerText;
}
item++;
}
di.vrList.Add(vr);
}

    public void UpdateDataBase()
    {
        string sqlstr;
        if (db.MyTable("select * from bbs where bbstitle like '" + di.bbstitle + "'").Rows.Count == 0)
        {
            sqlstr = "insert into bbs(bbstitle, bbsurl, bbsauthor, submittime, bbsview" + text + ", bbsreply" + text + ") values('" + di.bbstitle + "','" + di.bbsurl + "', '" + di.bbsauthor + "','" + di.submittime + "', '" + di.vrList[0].bbsview + "','" + di.vrList[0].bbsreply + "')";
        }
        else
        {
            sqlstr = "update bbs set bbsview" + text + "='" + di.vrList[0].bbsview + "',bbsreply" + text + "='" + di.vrList[0].bbsreply + "'" + "where bbstitle like '" + di.bbstitle + "'";
        }
        db.ExecuteCommand(sqlstr);
    }
longmansing
longmansing 这是后面的代码
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!