【问题标题】:Not able to Render YQL JSON Output using HTML / Javascript无法使用 HTML / Javascript 呈现 YQL JSON 输出
【发布时间】:2014-07-23 10:06:41
【问题描述】:

我正在使用XPATH 的以下YQL 查询来获取网页某些元素的数据:

select *
from html 
where url="http://www.desidime.com" 
and 
xpath='//h5[@class="product_text"]'

我试图在一个简单的 HTML 页面中显示结果,但我不知道为什么它不起作用。这一定是一些超级愚蠢的错误。

这是我的 HTML 页面的代码:

<html>
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <style type='text/css'>
        #results {
            width: 40%;
            margin-left: 30%;
            border: 1px solid gray;
            padding: 5px;
            height: 200px;
            overflow: auto;
        }
    </style>
    <script type='text/javascript'>
        // Parses returned response and extracts
        // the title, links, and text of each news story.
        function top_stories(o) {
            var output = '';
            var items = o.query.results.h5;
            var no_items = items.length;
            for (var i = 0; i < no_items; i++) {
                var title = items[i].a.content;
                output += "<h3>" + title + "</h3><hr/>";
            }
            document.getElementById('results').innerHTML = output;
        }
    </script>
</head>
<body>
    <!-- Div tag for stories results -->
    <div id='results'></div>
    ishan1
                <!-- The YQL statment will be assigned to src. -->
    <script src='https://query.yahooapis.com/v1/public/yql?q=select%20*%0Afrom%20html%20%0Awhere%20url%3D%22http%3A%2F%2Fwww.desidime.com%22%20%0Aand%20%0Axpath%3D\"%2F%2Fh5%5B%40class%3D%22product_text%22%5D\"&format=json&callback='></script>
</body>
</html>

我将不胜感激。

【问题讨论】:

  • define "not working",你有没有收到任何错误信息?

标签: javascript json xpath yql


【解决方案1】:

似乎编码的 XPath 查询是错误的。尝试打开您在浏览器中发布的 URL 时出现错误。尝试用这个替换它,这修复了错误并能够为我返回有效的 JSON 结果:

https://query.yahooapis.com/v1/public/yql?q=select+*+from+html+where+url%3d%22http%3a%2f%2fwww.desidime.com%22+and+xpath%3d%27%2f%2fh5[%40class%3d%22product_text%22]%27&format=json&callback=

【讨论】:

  • 做到了!非常感谢.. :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
  • 2021-04-26
  • 2019-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
相关资源
最近更新 更多