htmlagilitypack试图获取href内容的问题 [英] Problem with htmlagilitypack trying to get a href content

查看:84
本文介绍了htmlagilitypack试图获取href内容的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好亲爱的朋友

我有一个HTmlAgilityPac的问题。我有一个像这样的人:

hello dear freinds
i have a problem with HTmlAgilityPac. i have a pege like this:

<div Class="bd"><h3 class=""><a class="title main-headline" href="test">abolfaz jason</a></h3></div>





现在我需要访问href内部的内容(abolfazl jason)。我的代码是:



now i need access content of inside in a href ("abolfazl jason"). my code is:

Dim doc As New HtmlAgilityPack.HtmlDocument()
doc.LoadHtml("linkurl...")
Dim nodes As HtmlNode = doc.DocumentNode.SelectSingleNode("//div[@class='bd']//a")
Response.Write(nodes.InnerText)





但是应用程序运行有错误:



but application run has an error: "

Object reference not set to an instance of an object.





你可以帮帮我吗?



我尝试了什么:



HTmlAgilityPack试图获取href内容的问题

"

can you help me?

What I have tried:

Problem with HTmlAgilityPack trying to get a href content

推荐答案

您的代码有效。我把你的HTML字符串保存到我本地机器上的c:\ temp \ test.html。



Your code works. I took your HTML string and saved it to c:\temp\test.html on my local machine.

var html = @"c:\temp\test.html";

            var doc = new HtmlDocument();
            doc.Load(html);

            var nodes = doc.DocumentNode.SelectSingleNode("//div[@class='bd']//a");

            Console.WriteLine(nodes.InnerText);





但是,我在你的代码中看到你有< br $> b $ b



However, i see in your code you have

doc.LoadHtml("linkurl...")





您是否尝试下载HTML数据并从那里阅读?这可能是你的问题。



您应该使用WebClient下载链接。这样的事情。





Are you trying to download the HTML data and read it from there? This may be your problem.

You should use WebClient to download the link. Something like this.

var client = new WebClient();
            client.DownloadFile("http://link/to/your/stuff", @"c:\local\filename.html");





您也可以使用.DownLoad数据,将这些字节转换为流读取器,并使用HtmlDocument Load方法重载以从流中加载HTML。



但是假设代码是从本地文件中运行的。我相信您的空引用是由于尝试将URL视为本地文件。



You could also just use .DownLoad data, transform those bytes into a stream reader and user the HtmlDocument Load methods overload for loading HTML from a Stream.

But given that the code works from a local file. I believe your null reference is due to trying to treat a URL as a local file.


亲爱的David我测试您的解决方案。我稍微改变了我的代码。我认为你看到我的代码和主要网址和代码会更好:



Dear David i test your solution. i change my code a little. i think it is better you see my code and main url and code:

Dim Scr1 As New HtmlWeb()
Dim Url1 = Scr1.Load("https://www.linkedin.com/vsearch/p?openAdvancedForm=true&locationType=Y&f_I=47&rsid=4367283831473569842080&orig=ADVS")
Dim ournone As HtmlNode = Url1.DocumentNode.SelectSingleNode("//div[@class='bd']//a")
Response.Write(ournone.InnerHtml)





i还有问题!请帮帮我



i have problem yet! please help me


亲爱的朋友。我的Html Agility Pack存在严重问题。请查看我的代码:



在系统工作正确但不能使用相同内容在在线网址上运行的第一个代码,并返回空值:



是工作空值!

hello dear friend. i have a serious problem with Html Agility Pack. please see my code:

the first code when run on a saved file on system work correct but not work with the same content in a online url and return empty values:

is work empty value!
Dim Scr1 As New HtmlWeb()
Dim Url1 = Scr1.Load("https://www.linkedin.com/in/ladan-sahraei-14461b34?authType=OUT_OF_NETWORK&authToken=tK-W&locale=en_US&srchid=4367283831473659871577&srchindex=1&srchtotal=6693647&trk=vsrp_people_res_name&trkInfo=VSRPsearchId%3A4367283831473659871577%2CVSRPtargetId%3A120972292%2CVSRPcmpt%3Aprimary%2CVSRPnm%3Afalse%2CauthType%3AOUT_OF_NETWORK")
Dim ournone As HtmlNode = Url1.DocumentNode.SelectSingleNode("//span[@class='full-name']")
Response.Write(ournone.InnerHtml)





是工作正确!



is work correct!

Dim Scr1 As New HtmlWeb()
Dim Url1 = Scr1.Load("http://localhost:21374/HtmlPage.html")
Dim ournone As HtmlNode = Url1.DocumentNode.SelectSingleNode("//span[@class='full-name']")
Response.Write(ournone.InnerHtml)


这篇关于htmlagilitypack试图获取href内容的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆