Gre*_*egg 12

呼吁geturl()结果urllib2.urlopen().geturl()"返回检索到的资源的URL,通常用于确定是否遵循重定向."

例如:

#!/usr/bin/env python
# coding: utf-8

import urllib2

url1 = 'http://www.independent.co.uk/life-style/gadgets-and-tech/news/chinese-blamed-for-gmail-hacking-2292113.html'
url2 = 'http://www.independent.co.uk/life-style/gadgets-and-tech/news/2292113.html'

for url in [url1, url2]:
    result = urllib2.urlopen(url)
    print result.geturl()
Run Code Online (Sandbox Code Playgroud)

输出是:

http://www.independent.co.uk/life-style/gadgets-and-tech/news/chinese-blamed-for-gmail-hacking-2292113.html
http://www.independent.co.uk/life-style/gadgets-and-tech/news/chinese-blamed-for-gmail-hacking-2292113.html
Run Code Online (Sandbox Code Playgroud)

  • 正是我来这里写的.为了提高效率,请使用HEAD请求而不是加载页面的完整主体:http://stackoverflow.com/questions/107405/how-do-you-send-a-head-http-request-in-python/2070916#2070916 (4认同)