Python拓展16(urllib的quote处理url中的字符转义)

urllib的quote处理url中的字符转义

在url中,有些字符是需要转义的,如空格转义为%20", 引号转义为%22等。在urllib中可以用quote来进行处理。

def getPage():
    try:
        base = "http://www.totallyfreestuff.com/"
        href = "/95748/0/1/\"We_Have_Choices\"_DVD"
        url = base + urllib.quote(href)
        print url
        response = urllib.urlopen(url)  
        webPage = response.read()  
        return webPage
    except Exception, e:
        print "getPage function: ", e
        return None

注意:url中只需要quote需要转义的部分,不要把scheme、host等也转义了。

posted @ 2018-08-12 22:15  争-渡  阅读(1473)  评论(0)    收藏  举报