wd = input("输入关键字")
#get请求参数
data = {
'wd':wd,
'ie':'utf8'
}
url = 'http://www.baidu.com/s?'
#处理参数
data = urllib.parse.urlencode(data)
#拼接路径
url += data
#构建请求对象
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
request = urllib.request.Request(url=url,headers=headers)
#获取响应对象
response = urllib.request.urlopen(request)
#保存文件
with open('文件路径','wb') as fp:
fp.write(response.read())