我想匯入所有具有 [hreflang] 屬性的標簽。到目前為止,我只能使用以下公式匯入屬性值。
=IMPORTXML("https://allbirds.eu","//link[@hreflang]/@href")
現在我只獲取目標屬性值(例如 @href 在這種情況下:)https://fr-FR.allbirds.eu/。我無法匯入標簽本身。我想匯入標簽本身。我的預期結果是<link rel="alternate" hreflang="de-DE" href="https://de-DE.allbirds.eu/">
請給我您的建議,哪個 xpath 可以給我結果?
謝謝
uj5u.com熱心網友回復:
在您的情況下,如何使用 Google Apps 腳本如下?
示例腳本:
請將以下腳本復制并粘貼到電子表格的腳本編輯器中并保存腳本。當你使用這個腳本時,請把一個自定義函式=SAMPLE("https://allbirds.eu")放到一個單元格中。
const SAMPLE = url => [...UrlFetchApp.fetch(url).getContentText().matchAll(/<link[\w\s\S]. ?>/g)].flatMap(([e]) => e.includes("hreflang") ? [e] : []);
結果:

筆記:
- 此示例腳本適用于您的
https://allbirds.eu. 當您更改 URL 時,此腳本可能無法使用。請注意這一點。
參考:
- Google 表格中的自定義函式
添加:
我認為IMPORTXML不能直接檢索包含標簽之類的值<link rel="alternate" hreflang="de-DE" href="https://de-DE.allbirds.eu/">。所以我建議使用 Google Apps Script。但是,根據您的以下回復,
但我正在尋找 XPATH 的解決方案。Apps 腳本是我最后的選擇。
如果您需要使用IMPORTXML和 xpath,下面的示例公式怎么樣?
=ARRAYFORMULA("<link rel=""alternate"" hreflang="""&IMPORTXML("https://allbirds.eu","//link[@hreflang]/@hreflang")&""" href="""&IMPORTXML("https://allbirds.eu","//link[@hreflang]/@href")&""" />")
在這種情況下,可以獲得與上述示例 Google Apps 腳本相同的結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/432730.html
上一篇:MM:SS.SSS格式的總時間
下一篇:過濾谷歌表格中的多列不帶空格
