我无法从HtmlAgilityPack中获取NullReferenceExeption错误的网络数据

问题描述 投票:0回答:1

我无法使用XPath做任何事情,我不知道为什么这不起作用?我尝试了许多方法,但最终还是遇到了NullReferenceException错误。我真的需要您的帮助:/

您可以在此处查看完整尺寸的代码(https://notepad.pw/share/lyugaxmg

我尝试过这个;

  • 我使用ChroPath API,但这不起作用。
  • 我尝试右键单击或按F12,然后再次右键单击>复制>复制XPath,但是此操作不再起作用。

我尝试了很多方法,您能帮我吗?我可能不知道我的代码有问题。

My Codes

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using HtmlAgilityPack;

namespace InternerVeriCekme
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public string html;
        public Uri url;

      public void VeriAl(string Url,string XPath,TextBox cikansonuc)
        {
            try
            {
                url = new Uri(Url);
            }
            catch (UriFormatException)
            {
            if (MessageBox.Show("Hatalı URL", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK) { };
        }
        catch(ArgumentNullException)
        {
            if (MessageBox.Show("Hatalı URL", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK) { };
        }

        WebClient client = new WebClient();
        client.Encoding = Encoding.UTF8;

        try
        {
            html = client.DownloadString(url);
        }
        catch (WebException)
        {
            if (MessageBox.Show("Hatalı URL", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK) { };
        }

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(html);


        try
        {
            textBox1.Text = doc.DocumentNode.SelectSingleNode(XPath).InnerText;
        }
        catch (NullReferenceException)
        {
            if (MessageBox.Show("Hatalı xpath", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK) { };
        }
  }


    private void button1_Click(object sender, EventArgs e)
    {
        if (bunifuMaterialTextbox1.Text != "")
        {
            VeriAl("https://www.instagram.com/lunaslol/", "/html[1]/body[1]/div[1]/section[1]/main[1]/div[1]/div[1]/h1[1]", textBox1);

        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }
}

}

c# xpath system.net
1个回答
0
投票
  1. 当您在这里尝试创建一个动态源的社交媒体网站时,即使您刷新页面,也必须了解所尝试的xpath可以使用。

  2. ChroPath生成具有稳定属性的相对xpath,因此您可以决定哪些属性不是动态的,并在ChroPath中使用它们来生成稳定的相对xpath。

  3. 请按照this video tutorial了解如何生成具有稳定属性的相对xpath。

© www.soinside.com 2019 - 2024. All rights reserved.