「ascii」codec can't encode characters in position 32-34:ordinal not in range(128)の問題の解決

2029 ワード

                     UnicodeEncodeError: 'ascii' codec can't encode characters in position 32-34: ordinal not in range(128)  
               .
urllib.request.quote(key)
        ,         URL           .        
 
  
# -*- coding: utf-8 -*-
import urllib.request

url="http://search.51job.com/jobsearch/search_result.php?fromJs=1&jobarea=040000&keyword="
key="Python     "
key_code=urllib.request.quote(key)
url_all=url+key_code
req=urllib.request.Request(url_all)

headers=("User-Agent"," Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 QIHU 360SE",)
opener=urllib.request.build_opener()
opener.addheaders = [headers]
data=opener.open(req).read()

fhandle=open("d:\cdd.html","wb")
fhandle.write(data)
fhandle.close()
print (data)