【问题标题】:Retrieving the contents of an html label using XPath使用 XPath 检索 html 标签的内容
【发布时间】:2016-05-15 03:48:28
【问题描述】:

我有以下html代码:

<div id="ipsLayout_contentArea">
<div class="preContentPadding">
<div id="ipsLayout_contentWrapper">
<div id="ipsLayout_mainArea">
<a id="elContent"></a>
<div class="cWidgetContainer " data-widgetarea="header" data-orientation="horizontal" data-role="widgetReceiver" data-controller="core.front.widgets.area">
<div class="ipsPageHeader ipsClearfix">
<div class="ipsClearfix">
<div class="cTopic ipsClear ipsSpacer_top" data-feedid="topic-100269" data-lastpage="" data-baseurl="https://forum.com/forum/topic/100269-topic/" data-autopoll="" data-controller="core.front.core.commentFeed,forums.front.topic.view">
<div class="" data-controller="core.front.core.moderation" data-role="commentFeed">
<form data-role="moderationTools" data-ipspageaction="" method="post" action="https://forum.com/forum/topic/100269-topic/?csrfKey=b092dccccee08fdbc06c26d350bf3c2b&do=multimodComment">
<a id="comment-626016"></a>
<article id="elComment_626016" class="cPost ipsBox ipsComment ipsComment_parent ipsClearfix ipsClear ipsColumns ipsColumns_noSpacing ipsColumns_collapsePhone " itemtype="http://schema.org/Comment" itemscope="">
<aside class="ipsComment_author cAuthorPane ipsColumn ipsColumn_medium">
<div class="ipsColumn ipsColumn_fluid">
<div id="comment-626016_wrap" class="ipsComment_content ipsType_medium ipsFaded_withHover" data-quotedata="{"userid":3859,"username":"Admin","timestamp":1453221383,"contentapp":"forums","contenttype":"forums","contentid":100269,"contentclass":"forums_Topic","contentcommentid":626016}" data-commentid="626016" data-commenttype="forums" data-commentapp="forums" data-controller="core.front.core.comment">
<div class="ipsComment_meta ipsType_light">
<div class="cPost_contentWrap ipsPad">
<div class="ipsType_normal ipsType_richText ipsContained" data-controller="core.front.core.lightboxedImages" itemprop="text" data-role="commentContent">
<p> Hi, </p>
<p>   </p>
<p> This is a post with multiple </p>
<p> lines of text </p>

我正在尝试获取帖子的内容(纯文本)。我当前使用的 XPath:

//div[@id='ipsLayout_contentArea']/div[2]/div/div[4]/div/form/article/div/div/div[2]/div//text()

检索每个帖子的每一行(由&lt;p&gt;&lt;/p&gt; 分隔)。如何获取帖子的全部内容(内:

<div class="ipsType_normal ipsType_richText ipsContained" data-controller="core.front.core.lightboxedImages" itemprop="text" data-role="commentContent"> Post content </div>), 

纯文本(以便&lt;p&gt;&lt;/p&gt; 被视为文本(以及帖子可能包含的其他标签))?

编辑:

我正在使用以下 XPath:

//div[@id='ipsLayout_contentArea']/div[2]/div/div[4]/div/form/article/div/div/div[2]/div

检索包含帖子正文的 div。

// forumTemplate.getXpathElements().get(forumTemplate.XPATH_GET_THREAD_POSTS) = //div[@id='ipsLayout_contentArea']/div[2]/div/div[4]/div/form/article/div/div/div[2]/div
List<DomNode> posts = (List<DomNode>) firstPage.getByXPath(forumTemplate.getXpathElements().get(forumTemplate.XPATH_GET_THREAD_POSTS));
                for (DomNode post : posts) {
                    // Retrieve the contents of the post as a string
                    String postContentStr = post.getNodeValue();

变量postContentStr 始终为空。为什么?

【问题讨论】:

  • 这不能仅在 XPath 中完成。让您的 XPath 选择 div 并从 java 中获取 div 的内容作为文本(尽管对 java 部分没有帮助)
  • 我可以将 div 作为 dom 节点获取,但无法获取其值(所有标签都在其下方)。

标签: java html xpath


【解决方案1】:

您指定了//text(),它将递归获取指定路径下的所有文本节点。根据您的使用情况,这可能会更好:

//div[@data-role='commentContent']

这将与您尝试获取的评论节点相匹配。如果你使用代码来评估,你可以从这里开始。但是不要匹配text(),这不会匹配任何&lt;p&gt; 标签。

【讨论】:

  • 我不想渲染它,只渲染它的纯文本内容(它可能包含的所有标签都读取为文本,Java 中的字符串)。该文档是 html 页面而不是 xml。
  • 它是 html,但它也是 xml,因为您使用 xpath 处理它并构建一个 dom 树。因此,据我所知,您正在从 HTML 中构建一个 DOM 树,然后匹配该 DOM 中的特定节点。现在,您正尝试将 DOM 子树渲染回 HTML。关键是,XPath 不适用于“文本”级别,尽管我知道这是您最终想要的。
猜你喜欢
  • 1970-01-01
  • 2014-05-14
  • 2018-11-06
  • 2020-05-02
  • 1970-01-01
  • 2019-05-10
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多