栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从网址获取元数据

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从网址获取元数据

您不能仅仅因为这个

cross-origin
问题而做客户端。您需要服务器端脚本来获取页面的内容。

或者 您可以使用

YQL
。这样,
YQL
将用作代理。
https://policies.yahoo.com/us/en/yahoo/terms/product-
atos/yql/index.htm

或者, 您可以使用https://cors-anywhere.herokuapp.com。这样,任何地方的cors都将用作代理:

例如:

$('button').click(function() {  $.ajax({    url: 'https://cors-anywhere.herokuapp.com/' + $('input').val()  }).then(function(data) {    var html = $(data);    $('#kw').html(getmetaContent(html, 'description') || 'no keywords found');    $('#des').html(getmetaContent(html, 'keywords') || 'no description found');    $('#img').html(html.find('img').attr('src') || 'no image found');  });});function getmetaContent(html, name) {  return html.filter(  (index, tag) => tag && tag.name && tag.name == name).attr('content');}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type="text" placeholder="Type URL here" value="http://www.html5rocks.com/en/tutorials/cors/" /><button>Get meta Data</button><pre>  <div>meta Keyword: <div id="kw"></div></div>  <div>Description: <div id="des"></div></div>  <div>image: <div id="img"></div></div></pre>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399805.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号