在我的应用程序中,我使用雅虎的YQL API
从其他网站中提取HTML
,但雅虎停止了API,雅虎YQL API
提取HTML
将不再有效。< / p>
{
"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">
还有其他方法可以从其他网站HTML meta
中提取head
吗?
答案 0 :(得分:1)
http://developer.yahoo.com/yql/console/?q=select%20*%20from%20htmlstring%20where%20url%3D'YOUR_ENCODED_URL_HERE'&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
示例
http://developer.yahoo.com/yql/console/?q=select%20*%20from%20htmlstring%20where%20url%3D'http%3A%2F%2Fstackoverflow.com%2F'&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
REST查询
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20htmlstring%20where%20url%3D'http%3A%2F%2Fstackoverflow.com%2F'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
htmlstring是社区开放数据表的一部分。
答案 1 :(得分:0)
您可以使用queryselector读取元标记吗?我使用fetch来获取谷歌文档,它有助于拥有html元标记中的所有文档属性。然后我将html放入一个临时对象,我可以使用queryselector按我认为合适的方式。类似的东西:
var url = "https://docs.google.com/presentation/d/1blSsU5LHnrjSjb7voHXkRA_NlWo3yNjLiyttmoWfslM/edit#slide=id.gcb9a0b074_1_0"
var id = url.split("://")[1].split("/")[3];
var source = "https://docs.google.com/presentation/d/" + id + "/edit?usp=sharing";
fetch(source).then(function(response) {
return response.text();
}).then(function(html) {
var doc = document.implementation.createHTMLDocument("foo");
doc.documentElement.innerHTML = html;
return doc.querySelector("meta[property='og:description']").getAttribute("content");
}).then(function(title) {
console.log("document title", title);
});