在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等也转义了。
本文详细解释了URL中字符转义的必要性,并通过实例展示了如何使用urllib模块进行URL处理。
905

被折叠的 条评论
为什么被折叠?



