有在用 yahoo pipes 的吗, 想知道 XPath fetch page 模块里的 emit items as string 功能 , 用 YQL 怎么写 ? ?

2012-12-29 12:31:34 +08:00
 reloop
5527 次点击
所在节点    问与答
17 条回复
toothpaste
2012-12-29 14:11:08 +08:00
选择默认输出的XML格式,返回的结果就是 string 呀.
reloop
2012-12-29 14:37:07 +08:00
@toothpaste
终于有人回复了!! 我的意思是 , YQL 怎么写能返回像这样的 ?
reloop
2012-12-29 14:49:03 +08:00
@toothpaste
select * from html where url="" and xpath=""
我这样写后得到的是个 DOM 结构

如果是一整篇文章, YQL 是不是可以像 xpath fetch 那样得到格式化的结果呢 ?

今天发现 charset 可以解决我的一些 feed 的乱码 问题 , 所以我想是不是也有什么东西可以让 YQL 模块输出格式化的内容

E文不好 , 在官方文档转了好几圈都没找到答案
toothpaste
2012-12-29 16:12:55 +08:00
select * from html where url="http://lifehacker.com/" and xpath="//*[contains(@class, 'splashposts')]"

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Flifehacker.com%2F%22%20and%20xpath%3D%22%2F%2F*[contains%28%40class%2C%20%27splashposts%27%29]%22&diagnostics=true

它返回的xml,在result 节点就是HTML呀. 不知道我有没有理解你的需求.
reloop
2012-12-30 00:06:38 +08:00
@toothpaste
恩 , 要怎么提取出这个 results 的内容 , 以 HTML 输出 ?
比如我把这部分直接做为 item.description 的 HTML 代码 , 或者使用 string build 再进一步格式化 ?
toothpaste
2012-12-30 07:07:11 +08:00
明白你的意思了,是要把YQL 用在Yahoo Pipes 里面是吧, 我以为你是直接想把YQL结果嵌在网页里.

这个问题我以前没遇到过, 搜索了一下,在SO 上有个方案: stackoverflow.com/questions/2567833

改了一下上面的例子,请看下面这个效果:

use "http://www.datatables.org/data/htmlstring.xml" as html.tostring;
select * from html.tostring where url="http://lifehacker.com/" and xpath="//*[contains(@class, 'splashposts')]"
reloop
2012-12-30 13:21:50 +08:00
@toothpaste
谢谢! 这个 html.tostring 跟 xpath fetch page 一样了
但是 , 当时使用 YQL 模块代替 xpath fetch page 模块是因为 YQL 可以像这样
select * from html where url="" and charset="" 来解决一些网页的乱码问题

现在 html.tostring 虽然可以按HTML格式化输出了 但是只能使用 url 和 xpath 两个参数啊 不支持 charset

因为我不知道怎么自己写数据表
就用你给的 http://www.datatables.org/data/htmlstring.xml 自己建了个数据表
简单加了条
<key id="charset" type="xs:string" paramType="variable" required="false" />

现在使用 charset 不会再输出 null 了
但还是乱码

我猜那个 html.tostring 里面的这段代码可能需要改
但实在看不懂 , 我可能又得滚去看很多不懂的东西了
如果你会改的话 能帮我写个支持charset 的 html.tostring 让我先用上么 ? 谢谢 !!

var results = y.rest(url).accept('text/html').get().response;
if (xpath) results = y.xpath(results, xpath);
response.object = results.toXMLString();
toothpaste
2012-12-30 19:26:20 +08:00
按照YQL的文档, http://developer.yahoo.com/yql/guide/yql-javascript-objects.html#yql-execute-restobject
它提供两个关于charset的方法, fallbackCharset(charset_list) 和 forceCharset(charset_list) 所以table改成这样:

var results = y.rest(url).forceCharset(charset).accept('text/html').get().response;
if (xpath) results = y.xpath(results, xpath);
response.object = results.toXMLString();

由于不知道你要处理的网址,我用 tianqi.cncn.com 试了一下 (话说现在找个不是utf-8的也挺不容易的) charset="utf-8" 时为乱码 而 charset="gb2312" 则可以正常显示, 说明这个改动是有作用的.

use "http://.../htmlstring.xml" as html.tostring;
select * from html.tostring where url="http://tianqi.cncn.com/" and charset="gb2312"
reloop
2012-12-30 23:45:52 +08:00
@toothpaste 问题解决了 , 多谢啊 , 做了好久无聊的测试... 哈哈 .. 非常感谢 !!!
reloop
2012-12-30 23:48:02 +08:00
@toothpaste 再谢一下 !!
toothpaste
2012-12-31 06:26:15 +08:00
@reloop 呵呵, 不客气,我也学到了东西.
woyer
2013-12-06 13:57:35 +08:00
最后一步怎么改table不太清楚,特定注册了个账号,又找不到楼主联系方式,冒昧来打扰下,希望得到回应。
reloop
2013-12-06 16:44:01 +08:00
@woyer 能说具体点么 , 我把 @ toothpaste 的代码贴上去就可以了
woyer
2013-12-06 17:25:19 +08:00
@reloop 感谢回应,具体就是我不太清楚这个代码该贴哪儿,
直接用这个,我测试了可以
use "http://www.datatables.org/data/htmlstring.xml" as html.tostring;
但看你们的回复,貌似是要新建个什么东西
reloop
2013-12-06 18:12:08 +08:00
@woyer
直接用 use "http://www.datatables.org/data/htmlstring.xml" as html.tostring;
是可以格式化为 HTML

然后我遇到乱码
所以需要 既格式化为 HTML 而且支持指定编码如 GB2312

一共修改两个地方
1. 在 inputs 里加上一个 key
<inputs>
<key id="charset" type="xs:string" paramType="variable" required="false" />
</inputs>
2. 是把代码贴到
<execute><![CDATA[
这里
]]></execute>
woyer
2013-12-06 18:25:33 +08:00
还是不太明吧,这个添加地方,是需要在YQL console里面新建一个table,再把您说的这些加上去么?
我也是到了可以格式化为HTML,但显示中文乱码的问题。
不好意思啊,确实对这块不是太熟,请有空再指点下。
woyer
2013-12-06 21:09:23 +08:00
做出来了自己研究了下,做成功了,再次感谢啊,您的思路启发了我。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/56140

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX