把百度搜索词变成网页
import urllib from urllib import request #urllib.parse.urlencode() # 完成一次get请求 headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"} url = "https://www.baidu.com/s?" keyword = input("请输入您要查询的信息:") wd = {"wd":keyword} wd = urllib.parse.urlencode(wd) #urlencode编码 fullUrl = url+wd print(fullUrl) # 获取到get请求的信息,写文件 req = request.Request(fullUrl,headers=headers) response = request.urlopen(req) with open("baiduSearch.html", "wb") as f: f.write(response.read())
效果:,同时会生成一个html网页,搜索的关键词就是海龟