python中文编码问题解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-14: ordinal n

编码报错:

https://www.baidu.com/s?wd=渴望飞的鱼
UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-14: ordinal not in range(128)

 

原因:

由于编码问题造成的,解决方法如下:

key=urllib.request.quote(keyworld)转换一下。
#http协议请求,客户端与服务端进行消息传递
#get请求
"""
在浏览器输入关键字,回车,url变化。
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu
&wd=hello&rsv_pq=f218eac10000df63&rsv_t=3615gHwrY%2FUiWRMS%2Fh3pT5KyEK755vjJ%2F2wKMOK%2BPwDS6uh0nWg%2BI32RUpU
&rqlang=cn&rsv_enter=1&rsv_sug3=7&rsv_sug1=8&rsv_sug7=101
得出,wd=hello,关键词是wd
可以简化为:https://www.baidu.com/s?wd=hello
"""
import urllib.request
keyworld="渴望飞的鱼"
key=urllib.request.quote(keyworld)
url="https://www.baidu.com/s?wd="+key
print(url)
req=urllib.request.Request(url)
data=urllib.request.urlopen(req).read()
fhandle=open('D:/爬虫/抓取文件/2018110205.html','wb')
fhandle.write(data)
fhandle.close()

运行结果:

D:\工具\pythonTools\CatchTest1101\venv\Scripts\python.exe D:/工具/pythonTools/CatchTest1101/venv/test/test110207.py
https://www.baidu.com/s?wd=%E6%B8%B4%E6%9C%9B%E9%A3%9E%E7%9A%84%E9%B1%BC

Process finished with exit code 0

同时生成成功

你可能感兴趣的:(python,爬虫)