【问题标题】:How to use python/PHP to remove redundancy in URL link?如何使用 python/PHP 去除 URL 链接中的冗余?
【发布时间】:2012-02-10 16:34:37
【问题描述】:

很多网站都会在url链接中添加标签以进行跟踪,例如

http://www.washingtonpost.com/blogs/answer-sheet/post/report-we-still-dont-know-much-about-charter-schools/2012/01/13/gIQAxMIeyP_blog.html?wprss=linkset&tid=sm_twitter_washingtonpost

如果我们删除附录“?wprss=linkset&tid=sm_twitter_washingtonpost”,仍然会转到同一页面。 是否有任何通用方法可以删除那些冗余元素?任何评论都会有所帮助。

谢谢!

【问题讨论】:

    标签: php python html url web


    【解决方案1】:

    要从 URL 中删除查询、片段部分

    在 Python 中使用urlparse

    import urlparse
     
    url = urlparse.urlsplit(URL)               # parse url
    print urlparse.urlunsplit(url[:3]+('','')) # remove query, fragment parts
    

    或者更轻量级的方法,但它可能不太通用:

    print URL.partition('?')[0]
    

    根据rfc 3986可以使用正则表达式解析URI:

    /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/
    

    因此,如果没有片段标识符(上述正则表达式中的最后一部分)或存在查询组件(第二部分到最后一部分),那么 URL.partition('?')[0] 应该可以工作,否则会回答拆分 '?' 上的 url会失败,例如,

    http://example.com/path#here-?-ereh
    

    urlparse 仍然回答works

    检查是否可以通过 URL 访问页面

    在 Python 中:

    import urllib2
    
    try:
        resp = urllib2.urlopen(URL)
    except IOError, e:
        print "error: can't open %s, reason: %s" % (URL, e)
    else:
        print "success, status code: %s, info:\n%s" % (resp.code, resp.info()),
    

    resp.read() 可用于读取页面内容。

    【讨论】:

      【解决方案2】:

      删除 URL 中的查询字符串:

      <?php
      $url = 'http://www.washingtonpost.com/blogs/answer-sheet/post/report-we-still-dont-know-much-about-charter-schools/2012/01/13/gIQAxMIeyP_blog.html?wprss=linkset&tid=sm_twitter_washingtonpost';
      $url = explode('?',$url);
      $url = $url[0];
      
      //check output
      echo $url;
      ?>
      

      检查网址是否有效:

      您可以使用 PHP 函数 get_headers($url)。示例:

      <?php
      //$url_o = 'http://www.washingtonpost.com/blogs/answer-sheet/post/report-we-still-dont-know-much-about-charter-schools/2012/01/13/gIQAxMIeyP_blog.html?wprss=linkset&tid=sm_twitter_washingtonpost';
      
      $url_o = 'http://mobile.nytimes.com/article?a=893626&f=21';
      
      $url = explode('?',$url_o);
      
      $url = $url[0];
      
      $header = get_headers($url);
      
      if(strpos($header[0],'Not Found'))
      {
          $url = $url_o;
      }
      
      //check output
      echo $url; 
      ?>
      

      【讨论】:

      • 谢谢 Zulkhaery。有时那些'?标签是必要的。如“mobile.nytimes.com/article?a=893626&f=21”。如果删除它,它将不会指向正确的页面。我的问题是如何知道它是否是多余的。谢谢
      • @user1063294:更新了我的答案以检查 URL 是否有效。
      【解决方案3】:

      你可以使用正则表达式:

      $yourUrl = preg_replace("/[?].*/","",$yourUrl);
      

      这意味着:“用空字符串替换问号和后面的所有内容”。

      【讨论】:

      • 谢谢埃雷尔。请参阅上面的我的 cmets。
      【解决方案4】:

      您可以制作一个 URL 解析器,它会从“?”中删除所有内容。和上

      <?php
      $pos = strpos($yourUrl, '?'); //First, find the index of "?"
      
      //Then, cut all the chars after the "?" and a append to a new URL string://
      $newUrl = substr($yourUrl, 0, -1*(strlen($yourUrl)-((int)$pos)));
      
      echo ($newUrl);
      ?>
      

      【讨论】:

        猜你喜欢
        • 2016-02-28
        • 1970-01-01
        • 2017-08-07
        • 1970-01-01
        • 2019-01-07
        • 1970-01-01
        • 1970-01-01
        • 2020-07-23
        • 1970-01-01
        相关资源
        最近更新 更多