与importXML一起使用的XPath表达式

编程入门 行业动态 更新时间:2024-10-25 07:24:31
本文介绍了与importXML一起使用的XPath表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

从网站 www.google.co.uk/finance?cid = 704531 我正在尝试导入范围"和"52周"字段以及该组中间的其他字段.我看过XPath,对此感到非常困惑.

From the website www.google.co.uk/finance?cid=704531 I'm trying to import the fields "Range" and "52 week" and the others in that group in the middle. I've looked at XPath and am very confused by it.

在单元格A1中,我有www.google.co.uk/finance?cid=704531.

In cell A1 I have www.google.co.uk/finance?cid=704531.

在B1单元格中有

=importxml(A1,//*[@id="market-data-div"]/div[2]/div[1]/table[1]/tbody/tr[4]/td[1])

但是结果是一个错误.

此HTML结构的正确XPath表达式是什么?

What's the correct XPath expression for this HTML structure?

推荐答案

Dom和<tbody/>

似乎您在Firebug或类似的开发人员工具中创建了XPath表达式. HTML不需要<tbody/>标记,而DOM则需要.而那些开发人员工具则在DOM上工作,因此将表行包装在这样的元素中.在查看页面源代码(由wget或其他工具(如有必要)获取)和Firebug呈现的结构时,您可以轻松地看到差异.

Dom and the <tbody/>

It seems you created that XPath expression within Firebug or similar developer tools. While HTML does not require <tbody/> tags, DOM does; and those developer tools work on the DOM and thus wrap the table rows in such an element. You can easily see the difference when looking at the page source (fetched by wget or other tools if necessary) and the structure presented by Firebug.

只需在表达式中省略/tbody轴步长即可:

Just omit the /tbody axis step in your expression:

//*[@id='market-data-div']/div[2]/div[1]/table[1]/tr[4]/td[1]

无论如何,我宁愿通过描述来获取单元格,这在措词上的更改较弱,但对结构上的更改却更为健壮:

Anyway, I'd rather go for fetching the cells by the description, which is a little weaker regarding changes on the wording, but much more robust to changes on the structure:

//*[@id='market-data-div']//tr[td = 'Vol / Avg.']/td[2]

引用表达式

第二个问题是您必须在XPath表达式中用双引号引起来,因此请使用:

Quoting the Expression

A second problem is you have to quote the XPath expression in double quotes, so for example use:

=importxml(A1,"//*[@id='market-data-div']//tr[td = 'Vol / Avg.']/td[2]")

更多推荐

与importXML一起使用的XPath表达式

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

发布评论

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

>www.elefans.com

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