在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等也转义了。