什么是 IMPORTXML 的 Xpath 以获得具有 @hreflang 属性的完整 <link> 标签

What is the Xpath to IMPORTXML to get the full <link> tag which have the @hreflang attribute

我想导入所有具有属性 [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可以给我结果?

谢谢

在您的情况下,使用 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。当您更改 URL 时,此脚本可能无法使用。请注意这一点。

参考:

已添加:

我以为IMPORTXML不能像<link rel="alternate" hreflang="de-DE" href="https://de-DE.allbirds.eu/">那样直接获取包含标签的值。所以我建议使用 Google Apps Script。但是,从您的以下回复中,

But I'm looking for a solution with XPATH. Apps Script is my last option.

如果需要使用IMPORTXML和xpath,下面的示例公式如何?

=ARRAYFORMULA("<link rel=""alternate"" hreflang="""&IMPORTXML("https://allbirds.eu","//link[@hreflang]/@hreflang")&""" href="""&IMPORTXML("https://allbirds.eu","//link[@hreflang]/@href")&""" />")

在这种情况下,可以获得与上述示例Google Apps Script相同的结果。