2014-07-23 47 views
0

我使用以下YQL查詢與XPATH得到一個網頁的某些元素的數據來渲染YQL JSON輸出:不能夠使用HTML/JavaScript的

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> 

我將不勝感激任何幫助。

+1

定義「* not working *」,您是否收到任何錯誤消息? – har07

回答

0

似乎編碼的XPath查詢是錯誤的。嘗試打開您在我的瀏覽器中發佈的網址時出現錯誤。嘗試用這個替換它,這修復了錯誤,並且能夠爲我返回有效的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= 
+0

這樣做!萬分感謝.. :) – Ishan