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=%E5%9F%8E%E4%B8%9C%E4%B9%A6%E9%99%A2
Process finished with exit code 0
同时生成成功