使用 Yahoo YQL 查询 html

2023-11-23

在尝试使用雅虎查询语言和 YQL 提供的 xpath 功能解析 html 时,我遇到了无法提取“text()”或属性值的问题。
例如
永久链接

select * from html where url="http://stackoverflow.com" 
and xpath='//div/h3/a'

给出 xml 形式的锚点列表

<results>
    <a class="question-hyperlink" href="/questions/661184/filling-the-text-area-with-the-text-when-a-button-is-clicked" title="In ASP.net, I need the code to fill the text area (in the form) when a button is clicked. Can you help me through by showing a simple .aspx code containing the script tag? ">Filling the text area with the text when a button is clicked</a>...
</results> 

现在,当我尝试使用提取节点值时

select * from html where url="http://stackoverflow.com" 
and xpath='//div/h3/a/text()'

我得到的是串联结果而不是节点列表 例如

<results>Xcode: attaching to a remote process for debuggingWhy is b
…… </results>

我如何将其分成节点列表我该如何选择属性值 ?

像这样的查询

select * from html where url="http://stackoverflow.com"
and xpath='//div/h3/a[@href]'

给了我相同的查询结果div/h3/a


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 有更多疑问,请告诉我。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Yahoo YQL 查询 html 的相关文章