简体   繁体   English

如何在 Google 表格中为 IMPORTXML 找出正确的 xpath - 收到错误?

[英]How to figure out proper xpath for IMPORTXML in Google Sheets - Receiving An Error?

I'm trying to use the IMPORTXML function on Google Sheets.我正在尝试在 Google 表格上使用 IMPORTXML function。

For example:=IMPORTXML("https://exolyt.com/user/amazonprimevideo/full, XMLPATH) should return "1.8K", which is the Avg. Video Shares例如:=IMPORTXML("https://exolyt.com/user/amazonprimevideo/full, XMLPATH) 应返回“1.8K”,即平均视频份额

I used the Chrome inspector to copy the xpath, which gives me:我使用 Chrome 检查器复制了 xpath,它给了我:

/html/body/div[1]/div[1]/div/div/div/div[3]/div[1]/div/div[2]/div/div[1]/div[3]/div[6]/div[3]/div[2] /html/body/div[1]/div[1]/div/div/div/div[3]/div[1]/div/div[2]/div/div[1]/div[3]/格[6]/格[3]/格[2]

When I try this in Google Sheets it returns an error: #N/A (Import Content is Empty).当我在 Google 表格中尝试此操作时,它返回错误:#N/A(导入内容为空)。

PS I'm open to other ways to get the data I need into the google sheet, it doesn't have to use the IMPORTXML function. PS我愿意通过其他方式将我需要的数据输入谷歌表格,它不必使用IMPORTXML function。

I believe your goal as follows.我相信你的目标如下。

  • You want to retrieve the value of "Avg. Video Shares".您想要检索“Avg. Video Shares”的值。
  • For example, in this case, 1.8K is the value you need.例如,在这种情况下, 1.8K是您需要的值。

For this, I would like to propose the following modified formula.为此,我想提出以下修改后的公式。

Modified formula:修改公式:

=IMPORTXML(A1,"//div[../div[text()='Avg. video shares']][2]")
  • In this case, the URL of https://exolyt.com/user/amazonprimevideo/full is put to the cell "A1".在这种情况下,将https://exolyt.com/user/amazonprimevideo/full的 URL 放入单元格“A1”。
  • I used //div[../div[text()='Avg. video shares']][2]我用//div[../div[text()='Avg. video shares']][2] //div[../div[text()='Avg. video shares']][2] as the xpath. //div[../div[text()='Avg. video shares']][2]为 xpath。

Result:结果:

在此处输入图像描述

Note:笔记:

  • As other xpath, in your case, you can also use /html/body/div[1]/div/div/div/div/div/div[1]/div/div[2]/div/div[1]/div[2]/div[5]/div[3]/div[2] as the xpath like below.与其他 xpath 一样,在您的情况下,您也可以使用/html/body/div[1]/div/div/div/div/div/div[1]/div/div[2]/div/div[1]/div[2]/div[5]/div[3]/div[2]作为 xpath 如下所示。

     =IMPORTXML(A1,"/html/body/div[1]/div/div/div/div/div/div[1]/div/div[2]/div/div[1]/div[2]/div[5]/div[3]/div[2]")
  • From I'm open to other ways to get the data I need into the google sheet, it doesn't have to use the IMPORTXML function.I'm open to other ways to get the data I need into the google sheet, it doesn't have to use the IMPORTXML function. , if you want to achieve your goal using Google Apps Script, you can also use the following script as the custom function. ,如果您想使用 Google Apps Script 实现您的目标,您还可以使用以下脚本作为自定义 function。 In this case, please copy and paste the following script to the container-bound script of Spreadsheet, and put =customFunction() to a cell.在这种情况下,请将以下脚本复制并粘贴到电子表格的容器绑定脚本中,并将=customFunction()放入单元格中。 By this, the value of 1.8K is retrieved.这样, 1.8K的值就被检索到了。

     function customFunction() { const url = "https://exolyt.com/user/amazonprimevideo/full"; return UrlFetchApp.fetch(url).getContentText().match(/Avg\. video shares<\/div><div.+?>(\w.+?)<\/div>/)[1]; }
  • At above modified formula and sample script, when the URL is changed, the result you expect might not be able to be retrieved.在上面修改的公式和示例脚本中,当 URL 更改时,可能无法检索到您期望的结果。 So please be careful this.所以请注意这一点。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM