【问题标题】:How to get content of an HTML element using XPath without element id?如何使用没有元素 ID 的 XPath 获取 HTML 元素的内容?
【发布时间】:2016-04-13 04:30:41
【问题描述】:

我正在尝试使用 xpath 查找元素并获取元素文本值。请耐心等待并帮助我解决问题。

访问Click here

访问Click here

1

在<div class=“medium-8 columns”> - 我只需要提取到“Further History”的段落文本(即停在“Further History”,不包括“Further History”)。

2.

在<div class=“medium-8 columns”> - 这里我需要提取“Further History”之后的段落文本(不包括“Further History”)。

我正在使用下面的 XPath 表达式,它返回任何内容。

(//STRONG[not(contains(text(), 'Further History'))]/following-sibling::text() | //STRONG[not(contains(text(), 'Further History') )]/../following-sibling::p/text()) | //div[contains(@class, 'articlecontent')]

【问题讨论】:

标签: html xpath


【解决方案1】:

HTML 可能不区分大小写,但 XML(以及因此的 XPath)是:“STRONG”与“strong”不同,在您链接到的 HTML 中,只有“strong”。

检索您感兴趣的文本的有用 XPath 表达式可能是

//div[@class="medium-8 columns"]/p[following-sibling::p/strong]/text()

意思是

//div                           select all `div` elements, anywhere in the document
[@class="medium-8 columns"]     but only if they have a `class` attribute whose value is 
                                equal to "medium-8 columns"
/p                              of those `div` elements select all `p` child elements
[following-sibling::p/strong]   but only if they have a following sibling `p` which has a
                                `strong` element as a child
/text()                         of the remaining `p` elements, select the text content

哪个会返回(由------分隔的单个结果):

Tim Bajarin is recognized as one of the leading industry
consultants, analysts and futurists, covering the field of
personal computers and consumer technology. Mr. Bajarin has
been with Creative Strategies since 1981 and has served as a
consultant to most of the leading hardware and software
vendors in the industry including IBM, Apple, Xerox, Hewlett
Packard/Compaq, Dell, AT&T, Microsoft, Polaroid, Lotus,
Epson, Toshiba and numerous others.
-----------------------
His articles and/or analysis have appeared in USA Today, Wall
Street Journal, The New York Times, Time and Newsweek
magazines, BusinessWeek and most of the leading business and
trade publications. He has appeared as a business analyst
commenting on the computer industry on all of the major
television networks and was a frequent guest on PBS’ The
Computer Chronicles.
-----------------------
Mr. Bajarin has been a columnist for US computer industry
publications such as PC Week and Computer Reseller News and
wrote for ABCNEWS.COM for two years and Mobile Computing for
10 years. His columns currently appear in Asia Computer
Weekly, Personal Computer World (UK), and Microscope (UK) as
well as Mobile Enterprise Magazine. His various columns and
analyses are syndicated in over 30 countries.

对于你的第二种情况:

这里我需要提取“Further History”之后的段落文本(不包括“Further History”)

只需将路径表达式中的following-sibling 替换为preceding-sibling。

【讨论】:

  • @Nareshkumar:如果有帮助,请accept这个答案。谢谢。
猜你喜欢
  • 2015-11-06
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2020-07-20
  • 2011-07-10
  • 2015-12-12
  • 1970-01-01
相关资源
最近更新 更多