How cai i encode url
Peter Otten
__peter__ at web.de
Fri Aug 26 01:50:48 EDT 2011
John Smithury wrote:
> Hi pythons.
>
> following is my code
>
> # -*- coding: utf8 -*-
> import urllib2
> import urllib
>
> url = "http://a.shanting.mobi/百家讲坛/大国医/list"
> print url
> p1=u"百家讲坛".encode('utf8')
> p2=u"大国医".encode('utf8')
> encodeurl = "http://a.shanting.mobi/"+p1+"/"+p2+"/"+"list"
> print encodeurl
> mp3file = urllib2.urlopen(encodeurl)
> output = open("list1", "wb")
> output.write(mp3file.read())
> output.close
>
> -----------------------------------
> but error following, why? what can i encode the url?
The following seems to work:
# -*- coding: utf-8 -*-
import urllib2
url = u"http://a.shanting.mobi/百家讲坛/大国医/list"
encoded_url = urllib2.quote(url.encode("utf-8"), safe=":/")
instream = urllib2.urlopen(encoded_url)
with open("list1", "wb") as outstream:
outstream.write(instream.read())
More information about the Python-list
mailing list