0

I can't do anything using XPath, I don't know why this isn't working? I tried many ways still I ended up with NullReferenceException error. I really need your help :/

You can look at the code full size here (https://notepad.pw/share/lyugaxmg)

I tried this;

  • I take with ChroPath API but this does not work.
  • I tried to right-click or F12 and again right click>copy>copy XPath but this didn't work again.

I tried so many ways, can u help me? Maybe there is a problem with my code, I don't know.

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)
    {
    }
}

}

Abhishek Duppati
  • 554
  • 5
  • 18
  • Pasting your code within your question instead of attaching an image will make this much easier to read and respond to. Posting the stack trace would also be helpful. It would take only a few seconds if we knew the line number of the failure. – Michiel Bugher Apr 02 '20 at 23:02
  • My first guess would be that there is an exception thrown when you try to initialize the html field. Debug your code to see if this is the case. If it fails to initialize in your try block, it will still be null when you try to call doc. LoadHtml – Michiel Bugher Apr 02 '20 at 23:08
  • Instagram uses dynamic rendering. Not sure HtmlAgilityPack could deal with it. Please define what are you trying to scrape. – E.Wiest Apr 02 '20 at 23:32
  • I'm trying take Full Size Instagram Profile Picture , can you help me? – Burak Taha Apr 02 '20 at 23:49

0 Answers0