将表提取为JSON对象

时间:2016-02-11 15:50:33

标签: javascript json

我有一个解析网站的代码。现在我需要从网页中提取一个特定的表。我的代码如下:

<html>
<head>
<title>Pricing </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script>
<script>
//$(function(){
      function requestCrossDomain(site, callback) {
          if (!site) {
              alert('No site was passed.');
              return false;
          }
          var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=?';
          $.getJSON(yql, cbFunc);
          function cbFunc(data) {
              if (data.results[0]) {
                  data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
                      window[callback](data);
              } else throw new Error('Nothing returned from getJSON.');
          }
      }
      //$('#test').click(function(){
          var url = 'https://www.emcsg.com/marketdata/priceinformation';

          requestCrossDomain(url, 'someFunction');



function someFunction(results){
    console.log(results);
    $('#loadedContent').css("display","").html(results);  
}
</script>
</head>
<body>
<br><br>
<div id="result"></div>
<div id="loadedContent"></div>
</body>
</html>

解析的网页为https://www.emcsg.com/marketdata/priceinformation

网页上有几张表,但我需要提取一个特定的表“查看72个周期”。我检查了页面,表嵌套在各个类中。是否有一种简单的方法来提取表格?

1 个答案:

答案 0 :(得分:2)

这是:

var html = $(results);
var table = html.find(".view72PeriodsWrapper");

或者当您在评论中改变主意时:

var table = html.find(".realtimeTableContainer");

见行动:

//$(function(){
      function requestCrossDomain(site, callback) {
          if (!site) {
              alert('No site was passed.');
              return false;
          }
          var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=?';
          $.getJSON(yql, cbFunc);
          function cbFunc(data) {
              if (data.results[0]) {
                  data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
                      window[callback](data);
              } else throw new Error('Nothing returned from getJSON.');
          }
      }
      //$('#test').click(function(){
          var url = 'https://www.emcsg.com/marketdata/priceinformation';

          requestCrossDomain(url, 'someFunction');



function someFunction(results){
    var html = $(results);
    var table = html.find(".realtimeTableContainer");
    $('#loadedContent').css("display","").html(table);  
}
.realtimeTableHeaderContainer{display:none}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!--script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script-->
<div id="result"></div>
<div id="loadedContent"></div>