Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用没有元素id的XPath获取HTML元素的内容?_Html_Xpath - Fatal编程技术网

如何使用没有元素id的XPath获取HTML元素的内容?

如何使用没有元素id的XPath获取HTML元素的内容?,html,xpath,Html,Xpath,我正在尝试使用xpath查找元素并获取元素文本值。请容忍我,帮助我解决这个问题 拜访 拜访 1. 在-我只需要提取“进一步历史记录”之前的段落文本(即,在“进一步历史记录”处停止,不包括“进一步历史记录”) 2. 在-这里我需要提取“进一步的历史记录”(不包括“进一步的历史记录”)之后的段落文本 我使用下面的XPath表达式返回任何内容 (//STRONG[not(contains(text(),'Further History'))]/following同级::text()|///STRONG

我正在尝试使用xpath查找元素并获取元素文本值。请容忍我,帮助我解决这个问题

拜访

拜访

1. 在
-我只需要提取“进一步历史记录”之前的段落文本(即,在“进一步历史记录”处停止,不包括“进一步历史记录”)

2. 在
-这里我需要提取“进一步的历史记录”(不包括“进一步的历史记录”)之后的段落文本

我使用下面的XPath表达式返回任何内容

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


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
并返回(由
----
分隔的单个结果):


对于您的第二个案例:

在这里,我需要在“进一步的历史”(不包括“进一步的历史”)之后提取段落文本


只需将路径表达式中的
后面的兄弟姐妹
替换为
前面的兄弟姐妹

请注意前面的问题。接受或以其他方式对其作出反应。谢谢帮助:。与其问一个新的、类似的问题,不如对您在这里收到的精彩答案做出反应。如果它有帮助的话,它会变得更好;如果没有,请询问后续跟进。从中学习,而不是为你写作。谢谢。@Nareshkumar:如果有帮助,请回答这个问题。谢谢
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.