0

您好,我正在使用 yql 从外部网站获取网页内容。但是,尽管也使用了正确的 xpath 值和 foramt 作为 json,但我总是得到结果为 null。谁能帮我解决这个问题?我正在尝试获取以下网站的内容。如果 yql 有任何问题,谁能给我一些替代 yql 的建议?到目前为止,我已经尝试过了。请看一看。

var site = "http://www.amazon.in/Seiko-Premier-Analog-Blue-Watch/dp/B012T413GO?_encoding=UTF8&ref_=cm_sw_r_wa_apa_i_5c5uzbQG5A293";
site = site.replace('m.', '')
var yql = "SELECT * FROM html WHERE url='" + site + "' AND xpath='//title|//head/meta'";
var resturl = "http://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent(yql) + "&format=json";

$.getJSON(resturl,function(data){
    console.log(data);
})

http://www.amazon.in/Seiko-Premier-Analog-Blue-Watch/dp/B012T413GO?编码=UTF8&ref =cm_sw_r_wa_apa_i_5c5uzbQG5A293

4

1 回答 1

0

这是完整的示例,但首先您需要:

  • 使用 https(http 可能会返回 null 或错误)。
  • 请记住,您正在获取元标记,因此如果您需要或尝试在 HTML 中显示结果,您将什么也看不到,因此我使用控制台。

var site = "https://www.amazon.in/Seiko-Premier-Analog-Blue-Watch/dp/B012T413GO?encoding=UTF8&ref=cm_sw_r_wa_apa_i_5c5uzbQG5A293";
site = site.replace('m.', '');

var yql = "select * from htmlstring where url='" + site + "' AND xpath='//title|//head/meta'";

var resturl = "https://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent(yql) + "&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=";

$.getJSON(resturl, function(data) {
  console.log(data.query.results.result);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<div id="divContent"><i>Look the console - the results are not visible (they're only meta tags):</i></div>

于 2017-06-29T20:30:57.643 回答