Joe*_*haw 11 html xpath yahoo-pipes yql
我试图使用YQL从一系列网页中提取HTML的一部分.页面本身的结构略有不同(因此Yahoo Pipes"获取页面"及其"剪切内容"功能不能很好地工作)但我感兴趣的片段总是具有相同的class属性.
如果我有这样的HTML页面:
<html>
<body>
<div class="foo">
<p>Wolf</p>
<ul>
<li>Dog</li>
<li>Cat</li>
</ul>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
并使用如下的YQL表达式:
SELECT * FROM html
WHERE url="http://example.com/containing-the-fragment-above"
AND xpath="//div[@class='foo']"
Run Code Online (Sandbox Code Playgroud)
我得到的是(显然是无序的?)DOM元素,我想要的是HTML内容本身.我也试过SELECT content了,但那只选择了文字内容.我想要HTML.这可能吗?
您可以编写一个小的开放数据表来发送正常的YQL html表查询并将结果字符串化.类似于以下内容:
<?xml version="1.0" encoding="UTF-8" ?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<sampleQuery>select * from {table} where url="http://finance.yahoo.com/q?s=yhoo" and xpath='//div[@id="yfi_headlines"]/div[2]/ul/li/a'</sampleQuery>
<description>Retrieve HTML document fragments</description>
<author>Peter Cowburn</author>
</meta>
<bindings>
<select itemPath="result.html" produces="JSON">
<inputs>
<key id="url" type="xs:string" paramType="variable" required="true"/>
<key id="xpath" type="xs:string" paramType="variable" required="true"/>
</inputs>
<execute><![CDATA[
var results = y.query("select * from html where url=@url and xpath=@xpath", {url:url, xpath:xpath}).results.*;
var html_strings = [];
for each (var item in results) html_strings.push(item.toXMLString());
response.object = {html: html_strings};
]]></execute>
</select>
</bindings>
</table>
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用YQL查询查询该自定义表,如:
use "http://url.to/your/datatable.xml" as html.tostring;
select * from html.tostring where
url="http://finance.yahoo.com/q?s=yhoo"
and xpath='//div[@id="yfi_headlines"]/div[2]/ul/li'
Run Code Online (Sandbox Code Playgroud)
编辑:刚刚意识到这是一个非常古老的问题,被撞了; 至少答案就在这里,对于任何在这个问题上磕磕绊绊的人来说.:)
| 归档时间: |
|
| 查看次数: |
5425 次 |
| 最近记录: |