【问题标题】:Get site titles / descriptions for multiple urls获取多个网址的网站标题/描述
【发布时间】:2018-06-11 15:39:47
【问题描述】:

我正在尝试获取外部 url 列表的图标、站点标题和描述,最好使用 jquery。我已经成功地为我的网址同步了谷歌的网站图标服务,任何人都可以阐明如何为网站标题和描述实现这一目标吗?这是我到目前为止获得的图标。

HTML

<li><a href="http://dribbble.com/">Dribbble</a></li>
<li><a href="http://behance.net">Behance</a></li>
<li><a href="http://www.danwebb.net">Dan Webb</a></li>

JQUERY

$("a[href^='http']").each(function() {
$(this).prepend('<img src="https://www.google.com/s2/favicons? domain=' + this.href + '">');
});

【问题讨论】:

    标签: jquery html meta


    【解决方案1】:
    • 您有一个额外的空间? domain=
    • 另外,使用this.getAttribute("href") 或jQuery 的$(this).attr("href")
    • 使用https !!!

    $("a[href^='http']").each(function() {
       var href= this.getAttribute("href");
       $(this).prepend('<img src="https://www.google.com/s2/favicons?domain='+ href +'">';
    });
    <li><a href="https://dribbble.com/">Dribbble</a></li>
    <li><a href="https://behance.net">Behance</a></li>
    <li><a href="https://www.danwebb.net">Dan Webb</a></li>
    
    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>

    但你肯定会遇到这个错误

    ...已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'null' 不允许访问

    所以只使用 JS 是不可能的。


    PHP 救援:

    由于使用 AJAX 直接从不允许这样做的网站获取内容的 CORS 政策问题 - 您可以改为:

    1. AJAX GET 您的服务器上的一个 PHP 脚本,它将抓取外部站点
    2. 现在内容在您的域中,AJAX 将响应该内容。
    3. 使用 JS (jQuery) 解析响应数据以获取所需的元素属性值或文本。

    grabber.php

    <?php
        echo file_get_contents($_GET["url"]);
    

    index.html

    <ul>
        <li><a href="https://dribbble.com/">Dribbble</a></li>
        <li><a href="https://behance.net">Behance</a></li>
        <li><a href="https://www.danwebb.net">Dan Webb</a></li>
    </ul>
    
    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
    <script>
        function grabber (url, el) {
          $.ajax({
            url: "grabber.php?url="+ url,
            type: "GET",
            success: function(data){
              // console.log( data ); 
    
              var $head  = $(data).find("head");
              var title = $head.find("title").text();
              var desc  = $head.find("meta[name='author']").attr("content");
              console.log(title , desc);
              $(el).append(" - "+ title +" - "+ desc);
            }
          });
        }
    
        $("a[href^='http']").each(function () {
          var href= this.getAttribute("href");
          $(this).prepend('<img src="https://www.google.com/s2/favicons?domain='+ href +'">';
          grabber( href , this ); // this represent the current loop el.
        });
    <script>
    

    【讨论】:

    • 感谢您的编辑。我也想尝试从 url 中获取网站标题和描述?
    • 大多数网站会阻止您使用 AJAX 获取内容。
    • 啊,感谢您的帮助 Roko,我正在尝试获取摘要,前几行文本或任何对 url 稍有描述性的内容!我会继续寻找,除非你知道 PHP 的方法吗?
    • Roko 如何在每个链接前面加上标题和描述?
    • @JoelRosen 将当前循环元素传递给函数,例如:grabber( href, this );,而不是在grabber 函数内部(success)你可以这样做:$(el).append(" - "+ title +" " + desc); - 因此请确保还添加 el 参数,例如:function grabber (url, el) {
    猜你喜欢
    • 1970-01-01
    • 2014-10-31
    • 2011-12-03
    • 2022-08-09
    • 1970-01-01
    • 2013-06-12
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多