1 import urllib.request 2 url = "http://www.baidu.com/s?wd=" #get网址格式:“https://www.baidu.com/s?wd=关键字” 3 key="周杰伦" 4 key_code=urllib.request.quote(key) # 汉字编码转化 5 url_all=url+key_code 6 req=urllib.request.Request(url_all) 7 data=urllib.request.urlopen(req).read() 8 9 fhandle = open("F:/Python/5.html","wb") #把爬取到的数据,写入指定文件夹,并生成文件10 fhandle.write(data)11 fhandle.close()