簡體   English   中英

如何在 Google 表格中為 IMPORTXML 找出正確的 xpath - 收到錯誤?

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

我正在嘗試在 Google 表格上使用 IMPORTXML function。

例如:=IMPORTXML("https://exolyt.com/user/amazonprimevideo/full, XMLPATH) 應返回“1.8K”,即平均視頻份額

我使用 Chrome 檢查器復制了 xpath,它給了我:

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

當我在 Google 表格中嘗試此操作時,它返回錯誤:#N/A(導入內容為空)。

PS我願意通過其他方式將我需要的數據輸入谷歌表格,它不必使用IMPORTXML function。

我相信你的目標如下。

  • 您想要檢索“Avg. Video Shares”的值。
  • 例如,在這種情況下, 1.8K是您需要的值。

為此,我想提出以下修改后的公式。

修改公式:

=IMPORTXML(A1,"//div[../div[text()='Avg. video shares']][2]")
  • 在這種情況下,將https://exolyt.com/user/amazonprimevideo/full的 URL 放入單元格“A1”。
  • 我用//div[../div[text()='Avg. video shares']][2] //div[../div[text()='Avg. video shares']][2]為 xpath。

結果:

在此處輸入圖像描述

筆記:

  • 與其他 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]")
  • 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. ,如果您想使用 Google Apps Script 實現您的目標,您還可以使用以下腳本作為自定義 function。 在這種情況下,請將以下腳本復制並粘貼到電子表格的容器綁定腳本中,並將=customFunction()放入單元格中。 這樣, 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]; }
  • 在上面修改的公式和示例腳本中,當 URL 更改時,可能無法檢索到您期望的結果。 所以請注意這一點。

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM