How cai i encode url

John Smithury joho.smithury at gmail.com
Fri Aug 26 01:21:56 EDT 2011


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?

Traceback (most recent call last):
  File "D:/readurl.py", line 11, in <module>
    mp3file = urllib2.urlopen(encodeurl)
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 398, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 511, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 436, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 370, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 519, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110826/42ef260d/attachment.html>


More information about the Python-list mailing list