从html节点返回null [英] Returns a null from html node

查看:147
本文介绍了从html节点返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从页面



应该返回一个节点Cascade corporation的内联字母,但是我会得到null。

  HtmlNode htest = document.DocumentNode.SelectSingleNode(// * [@ id ='appbar'] / div / div [2] / div [1] / span) ; 

我缺少什么?

必须与Chrome一起使用

解决方案

我试图在我的机器上重现您的问题。我捕获了请求&响应数据使用Fiddler。我很惊讶地发现浏览器的HTML输出与我的代码不同。

从Fiddler我注意到的差异是用户代理的价值。后来我想出了这个代码,它适用于我。你可以请尝试一下,让我知道。请不要投票给我。我试图帮助你..

  string url =http://www.google.com/finance? q = NASDAQ:TXN&安培;文件系统类型= II; 
HtmlWeb web = new HtmlWeb();
web.UserAgent =Mozilla / 5.0(Windows NT 6.1; rv:12.0)Gecko / 20100101 Firefox / 12.0; //最新的firefox
HtmlDocument doc = web.Load(url);

var node = doc.DocumentNode.SelectSingleNode(// * [@ id ='appbar'] / div / div [2] / div [1] / span)
/ / var node = doc.DocumentNode.SelectSingleNode(// div [@ class ='appbar-snippet-primary'] / span)

当我评论用户代理行时,我能够重现您的问题。希望它有帮助。


I am attempting to access the company name from this page.

Should return a node with innertext of "Cascade corporation" however I get null instead.

HtmlNode htest = document.DocumentNode.SelectSingleNode("//*[@id='appbar']/div/div[2]/div[1]/span");

what am I missing?

P.S. must work with Chrome

解决方案

I tried to reproduce your issue on my machine. I captured request & response data using Fiddler. I was surprised to notice that rendered html output from browser is different from my code.

From Fiddler the difference I noticed is the user agent value. Later I came up with this code and it works for me. Can you please try it and let me know. Please don't down vote me. I'm trying to help you ya..

string url = "http://www.google.com/finance?q=NASDAQ:TXN&fstype=ii";
HtmlWeb web = new HtmlWeb();
web.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0"; // latest firefox 
HtmlDocument doc = web.Load(url);

var node = doc.DocumentNode.SelectSingleNode("//*[@id='appbar']/div/div[2]/div[1]/span")
//var node = doc.DocumentNode.SelectSingleNode("//div[@class='appbar-snippet-primary']/span")

When I comment user agent line I am able to reproduce your issue. Hope it helps.

这篇关于从html节点返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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