在Google文档中使用= importXML [英] Using =importXML in Google Docs

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

问题描述

从网站 https://www.google.co.uk/finance?cid = 704531 我试图导入字段范围和52周以及该组中的其他人。



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



在单元格B1中,

  = IMPORTXML(A1,// * [@ ID = 市场数据格] / DIV [2] / DIV [1] /表[1] / tbody的/ TR [4] / td [1])$ ​​b $ b  

但结果是错误。

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

解决方案



Dom和< tbody />



似乎你在Firebug或类似的开发者工具中创建了XPath表达式。虽然HTML不需要< tbody /> 标签,但DOM可以;而那些开发人员工具在DOM上工作,因此将表格行包装在这样的元素中。您可以在查看页面源代码(由 wget 或其他工具(如有必要)提取)和Firebug提供的结构中轻松查看其差异。



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

  

无论如何,我宁愿用描述获取单元格,这是一点点关于措辞上的变化较弱,但对结构上的变化强大得多:

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



引用表达式



第二个问题是您必须用双引号引用XPath表达式,例如使用:

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


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.

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

In cell B1 I have

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

But the result is an error.

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

解决方案

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.

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]

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]")

这篇关于在Google文档中使用= importXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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