【问题标题】:Fetching metadata from url从 url 获取元数据
【发布时间】:2016-06-24 23:44:04
【问题描述】:

我使用 Jsoup 库从 url 获取元数据。

Document doc = Jsoup.connect("http://www.google.com").get();  
String keywords = doc.select("meta[name=keywords]").first().attr("content");  
System.out.println("Meta keyword : " + keywords);  
String description = doc.select("meta[name=description]").get(0).attr("content");  
Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]");  

String src = images.get(0).attr("src");
System.out.println("Meta description : " + description); 
System.out.println("Meta image URl : " + src);

但我想在 client 端使用 javascript

【问题讨论】:

    标签: javascript html ajax cross-domain metadata


    【解决方案1】:

    纯Javascript函数

    从 node.js 后端(Next.js)我使用它:

    export const fetchMetadata = async (url) => {
        const html = await (await fetch(url, {
            timeout: 5000,
            headers: {
                'User-Agent': 'request'
            }
        })).text()
        
        var metadata = {};
        html.replace(/<meta.+(property|name)="(.*?)".+content="(.*?)".*\/>/igm, (m,p0, p1, p2)=>{ metadata[p1] = decode(p2) } );
        return metadata
    }
    
    export const decode = (str) => str.replace(/&#(\d+);/g, function(match, dec) {
        return String.fromCharCode(dec);
    })
    

    您可以通过https://cors-anywhere.herokuapp.com/corsdemo在客户端上使用它

    【讨论】:

      【解决方案2】:

      您不能仅仅因为 cross-origin 问题而在客户端执行此操作。您需要一个服务器端脚本来获取页面的内容。

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

      或者你可以使用https://cors-anywhere.herokuapp.com。这样,cors-anywhere 就会被用作代理:

      例如:

      $('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>

      【讨论】:

      • 感谢您的解决方案,但我如何才能显示来自 URL 的图像。 P.S URL 包含许多图片,如何展示其中最好的一张。
      • the best one from it 你怎么知道谁最厉害?
      • 这是一种稳定的解决方案,可以在社交网络中用于从 url 中抓取元数据,就像 facebook 一样?它可以处理许多并发请求吗?
      • @Engineeroholic 我没有通过很多请求对其进行测试。我确信 Facebook 不会这样做。 “正确”的解决方案是使用“代理”服务器。了解更多信息。阅读this答案。
      • 您要获取的网址是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 2013-05-06
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多