【问题标题】:Pulling specific key:value pairs from JSON url with javascript and CasperJS使用 javascript 和 CasperJS 从 JSON url 中提取特定的键:值对
【发布时间】:2017-01-19 00:28:57
【问题描述】:

说网址是:https://api.github.com/users/mralexgray/repos

我只是想提取“html_url”键的所有键:值对。

我一直在尝试使用以下内容:

function extractJSON() {
  $.ajax({
    url: "https://api.github.com/users/mralexgray/repos",
    dataType: 'json',
    success: function(data) { 
        alert(data.html_url);
        return data.html_url;
    };

  });

 // casperjs begins below

casper.start(https://api.github.com/users/mralexgray/repos, function() {
});

casper.then(function() {
var output = this.evaluate(extractJSON());
this.echo(output);
});

casper.run();

感谢您抽出宝贵的时间!

【问题讨论】:

    标签: javascript json casperjs


    【解决方案1】:

    可能的解决方案是抓取该站点的整个文本并将其解析为 json,然后您可以像这样轻松访问 json:

    var casper = require('casper').create();
    var url = 'https://api.github.com/users/mralexgray/repos';
    var text;
    var json;
    
    casper.start(url);
    
    casper.then(function() {
      text = casper.fetchText('body pre');
      json = JSON.parse(text);
    });
    casper.then(function() {
      for (var i=0; i < json.length; i++) {
        casper.echo("html_url: " + json[i].html_url);
        casper.echo("html_url owner: " + json[i].owner.html_url);
      }
    });
    
    casper.run();
    

    【讨论】:

    • 非常感谢您!非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2023-01-16
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    相关资源
    最近更新 更多