为什么Html.AgilityPack错过一些图像标记?

编程入门 行业动态 更新时间:2024-10-15 10:12:34
本文介绍了为什么Html.AgilityPack错过一些图像标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的HTML敏捷包,做了这样的事情

HtmlWeb网=新HtmlWeb();  的HTMLDocument文档= web.Load(test); 。诠释计数= doc.DocumentNode.SelectNodes(// IMG)计数();

我得到 38 回来了。

当我去到该页面,并做 $('IMG')的大小(); 我得到 43 回来。为什么有区别吗?为// IMG只是在寻找根的?

这就是为什么我可能会丢失一些?

解决方案   

时的// IMG只是在寻找   根的?

没有它寻找子节点(子女,孙子女,目前节点等)。在XPath EX pression从文档中选择所有的图片。

  

当我去到该页面,做$('IMG')的大小()。我得到43回来。

我的假设 - 一些图片是通过JavaScript动态创建。 HtmlAgilityPack不能处理这个问题。

顺便说一句,对于 test 我得到了87图像节点与AgilityPack( doc.DocumentNode.SelectNodes( // IMG)。COUNT()),并从丁目控制台87的图像节点( $('IMG')。大小() )。

修改: HtmlWeb.Load()方法在内部使用的 的WebRequest 类来获取数据。 AgilityPack的作用的正确解析数据的。这是完全可能的,一些网络资源换取相同的URI不同的内容取决于某些请求标头的如用户代理等。例如。 用户代理头可以通过 HtmlWeb.UserAgent 属性进行设置。

I am using the html agility pack and did something like this

HtmlWeb web = new HtmlWeb(); HtmlDocument doc = web.Load("test"); int count = doc.DocumentNode.SelectNodes("//img").Count();

I get 38 back.

When I go to that page and do $('img').size(); I get 43 back. Why is there a difference? Is "//img" just looking for root ones?

Is that why I might be missing some?

解决方案

Is "//img" just looking for root ones?

No it looking for descendant nodes (children, grandchildren, etc. of the current node). Your xpath expression selects all the images from the document.

When I go to that page and do $('img').size(); I get 43 back.

My assumption - some of the images are created dynamically via javascript. HtmlAgilityPack cannot handle this.

By the way, for the test I got 87 image nodes with AgilityPack (doc.DocumentNode.SelectNodes("//img").Count()), and 87 image nodes from the Chome console ($('img').size()).

EDIT: HtmlWeb.Load() method internally uses WebRequest class to get data. The role of AgilityPack is to parse the data correctly. It's completely possible that some web resources return different content for the same URI depending on some of request headers like User-Agent and others. E.g. User-Agent header could be set via HtmlWeb.UserAgent property.

更多推荐

为什么Html.AgilityPack错过一些图像标记?

本文发布于:2023-10-08 15:13:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472998.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   图像   Html   AgilityPack

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!