【发布时间】:2017-06-26 13:03:32
【问题描述】:
在我的应用程序中,我使用 Yahoo 的 YQL API 从其他网站提取 HTML,但 yahoo 停止了 API,Yahoo 用于提取 HTML 的 YQL API 将不再起作用。
{
"query": {
"count": 0,
"created": "2017-06-26T12:57:49Z",
"lang": "en-US",
"meta": {
"message": "html table is no longer supported. See https://policies.yahoo.com/us/en/yahoo/terms/product-atos/yql/index.htm for YQL Terms of Use"
},
"results": null
}
}
到目前为止,我是这样做的:
$(function () {
var fileFieldId;
var fileFieldClass;
var query;
var apiUrl;
$(".data-from-url").keyup(function () {
fileFieldId = $(this).attr('id');
fileFieldClass = $(this).attr('class');
fileFieldVal = $(this).val();
query = 'select * from html where url="' + $(this).val() + '" and xpath="*"';
apiUrl = 'https://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(query);
$.get(apiUrl, function(data) {
var html = $(data).find('html');
$("input.post[data-title='" + fileFieldId + "']" ).val(html.find("meta[property='og:title']").attr('content') || 'no title found');
$("textarea.post-description[data-description='" + fileFieldId + "']" ).val(html.find("meta[property='og:description']").attr('content') || 'no title found');
$("input.post-remote-image[data-img='" + fileFieldId + "']" ).val(html.find("meta[property='og:image']").attr('content') || '');
});
});
Here is a jsfiddle for call I am doing
$(function () {
var query;
var apiUrl;
$("button.click").click(function () {
//query = 'select * from htmlstring where url="' + $(this).val() + '" and xpath="//a"&format=json&env=store://datatables.org/alltableswithkeys&callback=';
apiUrl = "https://query.yahooapis.com/v1/public/yql?q=select * from htmlstring where url='http://stackoverflow.com/'&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&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 title 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') || '');
});
});
});
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">
还有其他方法可以从其他网站head 中提取HTML meta 吗?
【问题讨论】:
-
分享你用过的查询字符串。
-
创建服务器端爬虫
-
@CodeIt 我刚刚将查询添加到问题中
-
使用这个query我能够得到stackoverflow的完整html。如果可行,我会将其发布为答案。
-
感谢@CodeIt,感谢您的帮助。但是如果 API 出现故障,它会如何工作呢?是的,只要我能从
head中提取meta数据,我将不胜感激:)
标签: jquery ajax api yql yahoo-api