【问题标题】:Extracting HTML from XML or JSON output using yahoo's HTMLSTRING and get some details from xml or json output使用 yahoo 的 HTMLSTRING 从 XML 或 JSON 输出中提取 HTML 并从 xml 或 json 输出中获取一些详细信息
【发布时间】:2017-06-27 07:46:29
【问题描述】:

在我的应用程序中,我将使用Yahoo YQLhtmlstringxmljson 输出的网站中提取html

Ex XML 输出:https://query.yahooapis.com/v1/public/yql?q=select%20%2A%20from%20htmlstring%20where%20url%3D%27http%3A%2F%2Fstackoverflow.com%2F%27&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

EX JSON 输出:https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20htmlstring%20where%20url%3D%22http%3A%2F%2Fstackoverflow.com%2F%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=

我这样做的原因是为了得到它的property="og:image"property="og:title"property="og:image"

目前我正在这样做:

XML 输出:

  $(function () {
      var query;
      var apiUrl;      
      $("button.click").click(function () {
          
          apiUrl = "https://query.yahooapis.com/v1/public/yql?q=select * from htmlstring where url='http://stackoverflow.com/'&diagnostics=true&env=store://datatables.org/alltableswithkeys";

          $('p.extract').toggle();
          $.get(apiUrl, function(data) {
          	$('p.extract').addClass('none');
            var html = $(data).find('html');
            $("input.title" ).val(html.find("meta[property='og:title']").attr('content') || 'no description found');
           	 $("textarea.description").val(html.find("meta[property='og:description']").attr('content') || 'no title found');
            $("input.image").val(html.find("meta[property='og:image']").attr('content') || 'no image found');

      });

  });
    });
input {
    width: 100%;
    margin-bottom: 20px;
    padding: 10px;
}

.none{display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<button class="click">Click Me</button>
<br>
<p class="extract" style="display:none;">Extracting html</p>
<input type="text" class="title">
<br>
<textarea name="" id="" cols="30" rows="5" class="description"></textarea>
<br>
<input type="text" class="image">

JSON 输出:

  $(function () {

      var apiUrl;

      $("button.click").click(function () {
         
          apiUrl = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20htmlstring%20where%20url%3D%22http%3A%2F%2Fstackoverflow.com%2F%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";


          $('p.extract').toggle();
          $.get(apiUrl, function(data) {
          	$('p.extract').addClass('none');
            var html = $(data).find('html');
            $("input.title" ).val(html.find("meta[property='og:title']").attr('content') || 'no description found');
           	 $("textarea.description").val(html.find("meta[property='og:description']").attr('content') || 'no title found');
            $("input.image").val(html.find("meta[property='og:image']").attr('content') || 'no image found');

      });

  });
    });
input {
    width: 100%;
    margin-bottom: 20px;
    padding: 10px;
}

.none{display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="click">Click Me</button>
<br>
<p class="extract" style="display:none;">Extracting html</p>
<input type="text" class="title">
<br>
<textarea name="" id="" cols="30" rows="5" class="description"></textarea>
<br>
<input type="text" class="image">

我目前所做的并没有提供我想要的详细信息,即使我可以在输出中看到它们,我也找不到任何东西

感谢任何帮助,因为我不知道自己做错了什么。

【问题讨论】:

  • 您可以将data.results.result 转换为“可能是JSON”的对象吗?我感觉你在刮一根绳子……

标签: jquery html ajax yql yahoo-api


【解决方案1】:

因为我不能再相信 Yahoo 了,而且他们可能会阻止其他 api 主机,所以我选择了一个服务器端解决方案,内置在我的应用程序中。

我的应用程序基于 Ruby on Rails,并且在提交链接以显示实时结果时,我使用了Nokogiri 和对服务器的 ajax 调用。

【讨论】:

  • 您能否提供有关您创建的解决方案的更多详细信息?
  • @MauricioAriasOlave 不确定它是否适合您。我的应用程序基于 Ruby on Rails,当提交链接以显示 realtime 结果时,我使用了Nokogiri 和对服务器的 ajax 调用。如果你在 Rails 上使用 ruby​​ 或者有某种服务器端 html 解析器,我可以在这里提交 ajax 调用。
  • user6589814 感谢您的回复和您编辑的答案。赞成。我想通过 javascript/jquery 解决这个问题,但我还不能 :) 很高兴你找到了其他解决方案 :)
  • @MauricioAriasOlave 欣赏它。是的,我不再相信雅虎的 api,如果我是你,我也会使用一个服务器端解决方案,我可以完全控制自己:) 每种框架/语言都有很多 html 解析器,但 ajax 调用几乎是一样的。如果你愿意,我可以发给你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
相关资源
最近更新 更多