【问题标题】:Getting null result every time when trying to scrape web content using yql每次尝试使用 yql 抓取网页内容时都得到空结果
【发布时间】:2017-06-29 13:24:49
【问题描述】:

您好,我正在使用 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?encoding=UTF8&ref=cm_sw_r_wa_apa_i_5c5uzbQG5A293

【问题讨论】:

标签: html yql scrape web-content


【解决方案1】:

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

  • 使用 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>

【讨论】:

  • 我需要给出 url 的元描述、标题和图片(例如:如果是亚马逊链接,那么是产品图片)。你能帮我解决这个问题吗?我是yql的新手。所以对此没有太多想法。
  • @Sinchan,有了我的回答,现在您可以获得这些信息。根据您的个人资料,您了解javascript、jquery 等。您是否尝试“再次使用我的答案”获得所需的信息? = 你有没有尝试过别的?如果是这样,edit你的问题
  • 我希望你也看到了另一个你也回答过的问题。我改变了那个问题。这是该问题的链接:stackoverflow.com/questions/44804635/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多