YQL 需要 xpath 表达式来计算为 itemPath 而不是节点文本。但是一旦有了 itemPath,您就可以从树中投影各种值
换句话说,ItemPath 应该指向生成的 HTML 中的节点,而不是文本内容/属性。当您从数据中选择 * 时,YQL 将返回所有匹配的节点及其子节点。
example
select * from html where url="http://stackoverflow.com" and xpath='//div/h3/a'
这将返回与 xpath 匹配的所有 a。现在要投影文本内容,您可以使用以下命令将其投影出来
select content from html where url="http://stackoverflow.com" and xpath='//div/h3/a'
“content”返回节点内保存的文本内容。
为了投影出属性,您可以相对于 xpath 表达式指定它。在这种情况下,因为您需要相对于 a 的 href。
select href from html where url="http://stackoverflow.com" and xpath='//div/h3/a'
这返回<results>
<a href="/questions/663973/putting-a-background-pictures-with-leds"/>
<a href="/questions/663013/advantages-and-disadvantages-of-popular-high-level-languages"/>
....
</results>
如果您同时需要属性“href”和 textContent,则可以执行以下 YQL 查询:
select href, content from html where url="http://stackoverflow.com" and xpath='//div/h3/a'
returns:
<results> <a href="/questions/663950/double-pointer-const-issue-issue">double pointer const issue issue</a>... </results>
希望有帮助。如果您对 YQL 有更多疑问,请告诉我。