如何在 Google 表格中找出 IMPORTXML 的 xpath - 收到错误
How to figure out xpath for IMPORTXML in Google Sheets - Receiving An Error
提前谢谢大家的关注
我正在尝试在 Google 表格上使用 IMPORTXML 函数。
例如:=IMPORTXML("https://mg.olx.com.br/belo-horizonte-e-regiao/videogames/xbox-one-controle-hd-externo-975535277, XMLPATH) 应该return "(31) 98749-2542", 即完整的phone号。虽然为了访问完整号有一个“登录墙”,但是我直接在页面源代码上就可以看到完整号。
Firefox xPath Finder 扩展给我:
/html/body/div2/div/div[4]/div2/div/div2/div2/div[9]/div/div/div2/div/div/div/div/div[3]/div/a/div2
当我在 Google 表格中尝试此操作时,它 return 出现错误:#N/A(导入内容为空)。
请查看附件图片以了解我正在寻找什么。
full number on source code
非常感谢!
根据您提供的以下图片,
您要检索的值似乎在 JSON 对象中。而且,幸运的是,HTML 数据包括 JSON 数据。在这种情况下,为了实现您的目标,我想建议使用 Google Apps Script。示例脚本如下
示例脚本:
请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中并保存脚本。
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url);
const str = res.getContentText().match(/data-json\="(.+?)">/);
if (str && str.length == 2) {
const convert = [{from: """, to: '"'}, {from: ">", to: ">"}, {from: "<", to: "<"}, {from: "'", to: "'"}, {from: "&", to: "&"}];
const o = JSON.parse(convert.reduce((s, {from, to}) => s.replace(new RegExp(from, "g"), to), str[1]));
return o.ad && o.ad.phone && o.ad.phone.phone ? o.ad.phone.phone : "Value couldn't be found.";
}
return "Value couldn't be found.";
}
测试:
请将 =SAMPLE("https://mg.olx.com.br/belo-horizonte-e-regiao/videogames/xbox-one-controle-hd-externo-975535277")
的自定义函数放入单元格。这样,返回31987492542
。
注:
此示例脚本可用于 https://mg.olx.com.br/belo-horizonte-e-regiao/videogames/xbox-one-controle-hd-externo-975535277
的 URL 的当前 HTML 数据。当站点的规格改变并且 HTML 数据改变时,这个脚本可能无法使用。所以请注意这一点。
此示例脚本适用于 https://mg.olx.com.br/belo-horizonte-e-regiao/videogames/xbox-one-controle-hd-externo-975535277
的当前 URL。因此,当您更改 URL 时,此脚本可能无法使用。所以请注意这一点。
参考文献:
尝试
=REGEXEXTRACT(importxml(A1,"//script[@id='initial-data']/@data-json"),"(\d+)"",""phoneHidden")
说明
XPATH 在这里 //script[@id='initial-data']/@data-json
并将为您提供包含 json
中包含的所有数据的脚本
然后使用正则表达式获取phoneHidden之前的号码
提前谢谢大家的关注
我正在尝试在 Google 表格上使用 IMPORTXML 函数。
例如:=IMPORTXML("https://mg.olx.com.br/belo-horizonte-e-regiao/videogames/xbox-one-controle-hd-externo-975535277, XMLPATH) 应该return "(31) 98749-2542", 即完整的phone号。虽然为了访问完整号有一个“登录墙”,但是我直接在页面源代码上就可以看到完整号。
Firefox xPath Finder 扩展给我:
/html/body/div2/div/div[4]/div2/div/div2/div2/div[9]/div/div/div2/div/div/div/div/div[3]/div/a/div2
当我在 Google 表格中尝试此操作时,它 return 出现错误:#N/A(导入内容为空)。
请查看附件图片以了解我正在寻找什么。
full number on source code
非常感谢!
根据您提供的以下图片,
您要检索的值似乎在 JSON 对象中。而且,幸运的是,HTML 数据包括 JSON 数据。在这种情况下,为了实现您的目标,我想建议使用 Google Apps Script。示例脚本如下
示例脚本:
请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中并保存脚本。
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url);
const str = res.getContentText().match(/data-json\="(.+?)">/);
if (str && str.length == 2) {
const convert = [{from: """, to: '"'}, {from: ">", to: ">"}, {from: "<", to: "<"}, {from: "'", to: "'"}, {from: "&", to: "&"}];
const o = JSON.parse(convert.reduce((s, {from, to}) => s.replace(new RegExp(from, "g"), to), str[1]));
return o.ad && o.ad.phone && o.ad.phone.phone ? o.ad.phone.phone : "Value couldn't be found.";
}
return "Value couldn't be found.";
}
测试:
请将 =SAMPLE("https://mg.olx.com.br/belo-horizonte-e-regiao/videogames/xbox-one-controle-hd-externo-975535277")
的自定义函数放入单元格。这样,返回31987492542
。
注:
此示例脚本可用于
https://mg.olx.com.br/belo-horizonte-e-regiao/videogames/xbox-one-controle-hd-externo-975535277
的 URL 的当前 HTML 数据。当站点的规格改变并且 HTML 数据改变时,这个脚本可能无法使用。所以请注意这一点。此示例脚本适用于
https://mg.olx.com.br/belo-horizonte-e-regiao/videogames/xbox-one-controle-hd-externo-975535277
的当前 URL。因此,当您更改 URL 时,此脚本可能无法使用。所以请注意这一点。
参考文献:
尝试
=REGEXEXTRACT(importxml(A1,"//script[@id='initial-data']/@data-json"),"(\d+)"",""phoneHidden")
说明
XPATH 在这里 //script[@id='initial-data']/@data-json
并将为您提供包含 json
然后使用正则表达式获取phoneHidden之前的号码