问题
出错截图:
原因:
python不支持汉字 只支持ascii码
错误代码?
def get_page_with_para():para = "哈哈"url = "http://www.baidu.com/s?wd="url = url + paraprint(url)response = urllib.request.urlopen(url)data = response.read().decode("utf-8")with open("02.html","w",encoding="utf-8") as f:f.write(data)f.close()
get_page_with_para()
解决
导入包
import urllib.parse
import string
将url转译:
url = urllib.parse.quote(url,safe=string.printable)
成功将汉字转译:
http://www.baidu.com/s?wd=%E5%93%88%E5%93%88