HtmlAgilityPack NullReferenceException错误

时间:2019-01-07 21:49:24

标签: c# html-agility-pack

我正在尝试获取文本,但是我总是收到NullReferenceException错误。我快疯了。它工作了片刻,但我又开始再次收到此错误。我将获得帖子标题。

我尝试更改XPath。我尝试了不同的XPath。

    private void button1_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();

        for (int i = 4; i < 9; i++)
        {
            VeriAl(Url: "https://cracked.to/Forum-Combolists?sortby=started&order=desc&datecut=9999&prefix=0", XPath: "//table[@class='tborder clear']//tr[" + i + "]//td[2]//div[1]//span[1]//span[1]//a", tag: "title",CikanSonuc: listView1);
        }
    }

    public void VeriAl (String Url, String XPath, String tag,ListView CikanSonuc)
    {
        try
        {
            url = new Uri(Url);
        }
        catch (UriFormatException)
        {
            if (MessageBox.Show(text: "UriFormatException", caption: "Hata", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error) == DialogResult.OK)
            {

            }
        }
        catch (ArgumentNullException)
        {
            if (MessageBox.Show(text: "ArgumentNullException", caption: "Hata", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error) == DialogResult.OK)
            {

            }
        }

        WebClient client = new WebClient();
        try
        {
            html = client.DownloadString(url);
        }
        catch (WebException)
        {
            if (MessageBox.Show(text: "WebException", caption: "Hata", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error) == DialogResult.OK)
            {

            }
        }

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(html);
        try
        {
            CikanSonuc.Items.Add(doc.DocumentNode.SelectSingleNode(XPath).Attributes[tag].Value);
        }
        catch (NullReferenceException)
        {
            if (MessageBox.Show(text: "NullReferenceException", caption: "Hata", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error) == DialogResult.OK)
            {

            }
        }

2 个答案:

答案 0 :(得分:1)

问题在于,您尝试获取HTML时内容不存在。 如果调用WebClient.DownloadString(),则加载内容的Javascript代码不会执行。获取完全加载的网页的唯一方法是使用WebBrowser控件加载页面,并在加载完成后获取所有内容:

public static class WebViewExtension
{
    public static HtmlAgilityPack.HtmlDocument GetHtmlDocument(this WebBrowser wView)
    {
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(wView.Document.Body.OuterHtml);
        return doc;
    }

    public static async Task<HtmlAgilityPack.HtmlDocument> LoadSiteAndGetHtml(this WebBrowser wView, string siteurl)
    {
        await wView.NavigateAndWait(siteurl);
        HtmlAgilityPack.HtmlDocument doc = wView.GetHtmlDocument();
        return doc;
    }

    public static async Task NavigateAndWait(this WebBrowser wView, string siteurl)
    {
        TaskCompletionSource<bool> loaded = new TaskCompletionSource<bool>();
        wView.Navigate(new Uri(siteurl));
        wView.DocumentCompleted += delegate (object sender, WebBrowserDocumentCompletedEventArgs args)
        {
            loaded?.TrySetResult(true);
        };

        //wait until the website is loaded
        await loaded.Task;
    }
}

您可以使用以下方法:

    WebBrowser client = new WebBrowser();
    try
    {
        HtmlDocument doc = await client.LoadSiteAndGetHtml(url);
    }
    catch (WebException)
    {
        if (MessageBox.Show(text: "WebException", caption: "Hata", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error) == DialogResult.OK)
        {

        }
    }

答案 1 :(得分:0)

URL-https://cracked.to/Forum-Combolists?sortby=started&order=desc&datecut=9999&prefix=0显示加载屏幕几秒钟,并重定向到其自身,向您显示您想要查看的内容。在重定向之前,它还会设置一些浏览器cookie,以便您不会再看到加载屏幕,而是看到内容。重定向是使用脚本在客户端进行的。

在c#中,当您尝试下载页面时,它实际上是在下载该加载页面的源,而不是实际内容。您下载的HTML甚至没有您要访问的元素,因此存在null引用异常。

解决此问题将非常棘手,因为这是客户端重定向。您甚至可能最终会使用浏览器实例来抓取网页,以便在重定向后检索html。大概是这样的Scraping webpage generated by javascript with C#