import urllib.request
import urllib.parse
#https://www.baidu.com/s?ie=UTF-8&wd=中国
#将上面的中国部分内容,可以动态的变化内容、并编码,并得到html页面
#1 得到url地址
wd = input("请输入搜索内容:")
url = "http://www.baidu.com/s?" #完整url https://www.baidu.com/s?ie=UTF-8&wd=中国
#以字典形式保存url参数
data = {
"ie" : "UTF-8",
"wd" : wd
}
query_string = urllib.parse.urlencode(data)#用urlencode()方法拼接参数
url += query_string#合成完整url
# print(url)
#2 向url发送请求
response = urllib.request.urlopen(url)
filename = wd + ".html"
with open(filename, "wb") as html:
html.write(response.read())