有在用 yahoo pipes 的吗, 想知道 XPath fetch page 模块里的 emit items as string 功能 , 用 YQL 怎么写 ? ? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
reloop
V2EX    问与答

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

  •  
  •   reloop 2012 年 12 月 29 日 6029 次点击
    这是一个创建于 4830 天前的主题,其中的信息可能已经有所发展或是发生改变。
    17 条回复    1970-01-01 08:00:00 +08:00
    toothpaste
        1
    toothpaste  
       2012 年 12 月 29 日   1
    选择默认输出的XML格式,返回的结果就是 string 呀.
    reloop
        2
    reloop  
    OP
       2012 年 12 月 29 日
    @toothpaste
    终于有人回复了!! 我的意思是 , YQL 怎么写能返回像这样的 ?
    reloop
        3
    reloop  
    OP
       2012 年 12 月 29 日
    @toothpaste
    select * from html where url="" and xpath=""
    我这样写后得到的是个 DOM 结构

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

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

    E文不好 , 在官方文档转了好几圈都没找到答案
    toothpaste
        4
    toothpaste  
       2012 年 12 月 29 日   1
    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
        5
    reloop  
    OP
       2012 年 12 月 30 日
    @toothpaste
    恩 , 要怎么提取出这个 results 的内容 , 以 HTML 输出 ?
    比如我把这部分直接做为 item.description 的 HTML 代码 , 或者使用 string build 再进一步格式化 ?
    toothpaste
        6
    toothpste  
       2012 年 12 月 30 日   1
    明白你的意思了,是要把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
        7
    reloop  
    OP
       2012 年 12 月 30 日
    @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
        8
    toothpaste  
       2012 年 12 月 30 日   2
    按照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="" 则可以正常显示, 说明这个改动是有作用的.

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

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

    一共修改两个地方
    1. 在 inputs 里加上一个 key
    <inputs>
    <key id="charset" type="xs:string" paramType="variable" required="false" />
    </inputs>
    2. 是把代码贴到
    <execute><![CDATA[
    这里
    ]]></execute>
    woyer
        16
    woyer  
       2013 年 12 月 6 日
    还是不太明吧,这个添加地方,是需要在YQL console里面新建一个table,再把您说的这些加上去么?
    我也是到了可以格式化为HTML,但显示中文乱码的问题。
    不好意思啊,确实对这块不是太熟,请有空再指点下。
    woyer
        17
    woyer  
       2013 年 12 月 6 日
    做出来了自己研究了下,做成功了,再次感谢啊,您的思路启发了我。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2670 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 48ms UTC 04:16 PVG 12:16 LAX 21:16 JFK 00:16
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86