C#HTML Agility Pack(SelectSingleNode) [英] C# Html Agility Pack ( SelectSingleNode )
问题描述
我正在尝试解析此字段,但无法使其正常工作.当前尝试:
I'm trying to parse this field, but can't get it to work. Current attempt:
var name = doc.DocumentNode.SelectSingleNode("//*[@id='my_name']").InnerHtml;
<h1 class="bla" id="my_name">namehere</h1>
错误:对象引用未设置为对象的实例.
Error: Object reference not set to an instance of an object.
感谢任何帮助.
@John-我可以确保HTML正确加载.我正在尝试读取我的Facebook名称以供学习.这是Firebug插件的屏幕截图.我使用的版本是1.4.0.
@John - I can assure that the HTML is correctly loaded. I am trying to read my facebook name for learning purposes. Here is a screenshot from the Firebug plugin. The version i am using is 1.4.0.
http://i54.tinypic.com/kn3wo.jpg
我想问题是profile_name是一个子节点或其他东西,这就是为什么我无法读取它的原因?
I guess the problem is that profile_name is a child node or something, that's why I'm not able to read it?
推荐答案
代码不起作用的原因是因为页面上有JavaScript实际上正在写出<h1 id='profile_name'>
标记,因此如果您请求没有执行JavaScript的用户代理(或通过AJAX)显示的页面,则找不到该元素.
The reason your code doesn't work is because there is JavaScript on the page that is actually writing out the <h1 id='profile_name'>
tag, so if you're requesting the page from a User Agent (or via AJAX) that doesn't execute JavaScript then you won't find the element.
我可以使用以下选择器获得自己的名字:
I was able to get my own name using the following selector:
string name =
doc.DocumentNode.SelectSingleNode("//a[@id='navAccountName']").InnerText;
这篇关于C#HTML Agility Pack(SelectSingleNode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!