如何从html源代码获取元图像?

时间:2017-05-09 11:31:04

标签: jquery

我在控制台中有html数据....我如何从这个html源代码中找到$select->combine($subSelect); 以下代码采用字符串

的形式
meta tag og image

2 个答案:

答案 0 :(得分:3)

如果您将此标记作为字符串,则需要将其解析为HTML文档 为此,请使用DOMParser API

从那时起,您可以使用普通的DOM方法转到元素并获取其属性值:

var markup = `<!DOCTYPE html><html lang="en" id="facebook" class="no_js"><head><meta charset="utf-8" /><meta name="referrer" content="origin-when-crossorigin" id="meta_referrer" /><script>function envFlush(a){function b(c){for(var d in a)c[d]=a[d];}if(window.requireLazy){window.requireLazy(['Env'],b);}else{window.Env=window.Env||{};b(window.Env);}}envFlush({"ajaxpipe_token":"AXjKg9wJIltZEBjN","khsh":"0\`sj\`e\`rm\`s-0fdu^gshdoer-0gc^eurf-3gc^eurf;1;enbtldou;fduDmdldourCxO\`ld-2YLMIuuqSdptdru;qsnunuxqd;rdoe-0unjdojnx-0unjdojnx0-0gdubi^rdbsduOdv-0\`sj\`e\`r-0q\`xm\`r-0StoRbs\`qhof-0mhoj^q\`xm\`r"});<\/script><style></style><script>__DEV__=0;CavalryLogger=false;<\/script><noscript><meta http-equiv="refresh" content="0; URL=/?_fb_noscript=1" /></noscript><title id="pageTitle">Facebook - Log In or Sign Up</title><meta property="og:site_name" content="Facebook" /><meta property="og:url" content="https://www.facebook.com/" /><meta property="og:image" content="https://www.facebook.com/images/fb_icon_325x325.png" /><meta property="og:locale" content="en_GB" /><script type="application/ld+json">{"\u0040context":"http:\/\/schema.org","\u0040type":"WebSite","name":"Facebook","url":"https:\/\/www.facebook.com\/"}`;

var doc = new DOMParser().parseFromString(markup, 'text/html');
var meta = doc.querySelector('meta[property="og:image"]');
var value = meta && meta.getAttribute('content');

console.log(value);

答案 1 :(得分:0)

您需要为元标记添加name属性,然后:

$("meta[name=og-image]").attr("content")