【问题标题】:Can we get the attribute names themselves from Sheets importxml我们可以从 Sheets importxml 中获取属性名称本身吗
【发布时间】:2021-07-26 08:42:54
【问题描述】:

我正在尝试使用 Google 表格 IMPORTXML 函数从网站获取数据属性列表。澄清一下,我想要属性本身的 名称 以及它们的值,我不需要表格本身的文本。

试用网站:https://thesilphroad.com/catalog

我想从中提取的代码示例是:

<div class="pokemonOption sighted" data-nests="1" data-raid-boss="0" data-obtainable="1" data-released="1" data-shiny-obtainable="1" data-shiny-released="1" data-shadow-available="" data-shadow-released="1" data-pokemon-slug="bulbasaur" style="background-image:url(https://assets.thesilphroad.com/img/pokemon/icons/96x96/1.png), radial-gradient(#a9f712, #2ecc71);"><span>#001</span></div>

我使用的公式是:

=importxml("https://thesilphroad.com/catalog","//div[@class='pokemonOption sighted']/@*

它返回1列中所有属性的值,例如:

attributes
pokemonOption sighted
1
1
Bulbasaur
etc...

但我还需要名称,例如data-nests="1"。或者,仅列出 that 的属性怎么样?

attribute names
class
data-nests
data-raid-boss
data-released
etc...

有人知道如何使用 IMPORTXML 或其他方法将其提取到表格中吗?

谢谢!

【问题讨论】:

  • 公式already seems to display the second table,这不是你想要的吗?
  • 哦,拍!感谢您注意到拉法。不知何故在编辑时我把它们放在了后面! 我现在已经编辑了我的问题,将它们转换为我的意思。因此,鉴于这一澄清,是否可以导入属性名称(类、数据嵌套等)以及导入或替代属性值?
  • 啊,在这种情况下,我认为这是不可能的,IMPORTXML 只获取值,可以通过IMPORTHTML 提取它们,但老实说,我什至不知道是否那会起作用的。
  • 谢谢@Rafa,我希望我只是错过了一些东西。手指交叉的表格最终会更新此功能

标签: web-scraping google-sheets google-sheets-formula


【解决方案1】:

我不太确定,但我认为IMPORTXML 使用XPath 1.0

要获取所有属性,您需要XPath 2.0

您可能希望使用 Google Apps 脚本创建自定义公式。

function pokemon(cell) {
  const url = 'https://thesilphroad.com/catalog';
  const response = UrlFetchApp.fetch(url);
  const content = response.getContentText();
  const results = content.match(/(<div class="pokemonOption sighted".+?<\/div>)/g)
  /* ... */
}

【讨论】:

  • 哇,谢谢!我对 Google Apps 脚本不是很有经验,所以我必须进行试验并回复您。但这作为一种解决方法非常有用。谢谢!
猜你喜欢
  • 2023-03-16
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多