通过 google-sheets 中的 xpath 在网站中搜索字符串后获取下一个文本的值

Get value of next text after search string in website via xpath in google-sheets

我正在使用 Google Sheet IMPORTHTML() 从网站获取文本。我正在尝试使用 XPath 在 Google Sheet 中通过 IMPORTHTML() 在某个搜索字符串 之后获取 文本。

例如,我想从以下网站中提取房间数和居住数 space https://www.comparis.ch/immobilien/details/favorite?id=26720381

https://www.comparis.ch/immobilien/details/favorite?id=26714976

当我通过 chrome 提取 xpath 时,不幸的是每个 url 并不总是完全相同。这就是为什么我想尝试搜索字符串:

//*[@id="__next"]/div/div[1]/div[1]/div[4]/div[1]/div[3]/div[1]/div/div/div[2]/div/div[1]/p
//*[@id="__next"]/div/div[1]/div[1]/div[4]/div[1]/div[3]/div[1]/div/div/div[2]/div/div[1]/p/span

因此,计划是在 A 列中包含 url 的列表,在第 1 行中包含相应的 xpath(包括搜索字符串,如“room”或“living space”和 table 其中 =importxml($A2,B$1) 将使用这些 urls 和 xpaths 来搜索相应的值。

我试图在找到字符串“room”或“living space”

的地方之后获取以下兄弟姐妹

非常感谢您的帮助,因为我是新手,现在迷路了。最好的,Ueli

尝试:

=VLOOKUP("Zimmer", IMPORTXML(A1, "//div[@class='css-1wfw5hl excbu0j4']"), 2, 0)

并向下拖动:

如何修改你的 xpath 如下?

修改后的 xpath:

//p[@class='css-1ush3w6 excbu0j2' and ../..//span[text()='Zimmer']]

结果:

当这个修改后的 xpath 用于你的 2 URLs 时,得到以下结果。

在此示例中,URL 和 =IMPORTXML(A1, "//p[@class='css-1ush3w6 excbu0j2' and ../..//span[text()='Zimmer']]") 分别放入单元格“A1”和“B1”。

参考: