与importXML一起使用的XPath表达式 [英] XPath expression to be used with importXML

查看:118
本文介绍了与importXML一起使用的XPath表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

From the website https://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中,我有https://www.google.co.uk/finance?cid=704531.

In cell A1 I have https://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表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆