from urllib import request from urllib import parse
url = 'http://www.baidu.com/s?wd={}' #想要搜索的内容 word = input('请输入搜索内容:') params = parse.quote(word) full_url = url.format(params)
#重构请求头 headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'} #创建请求对应 req = request.Request(url=full_url,headers=headers) #获取响应对象 res = request.urlopen(req) #获取响应内容 html = res.read().decode("utf-8")
filename = word + '.html' with open(filename,'w', encoding='utf-8') as f: f.write(html)完整程序如下所示:
from urllib import request,parse # 1.拼url地址 url = 'http://www.baidu.com/s?wd={}' word = input('请输入搜索内容:') params = parse.quote(word) full_url = url.format(params) # 2.发请求保存到本地 headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'} req = request.Request(url=full_url,headers=headers) res = request.urlopen(req) html = res.read().decode('utf-8') # 3.保存文件至当前目录 filename = word + '.html' with open(filename,'w',encoding='utf-8') as f: f.write(html)尝试运行程序,并输入编程帮,确认搜索,然后您会在 Pycharm 当前的工作目录中找到“编程帮.html”文件。
from urllib import request from urllib import parse # 拼接URL地址 def get_url(word): url = 'http://www.baidu.com/s?{}' #此处使用urlencode()进行编码 params = parse.urlencode({'wd':word}) url = url.format(params) return url # 发请求,保存本地文件 def request_url(url,filename): headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'} # 请求对象 + 响应对象 + 提取内容 req = request.Request(url=url,headers=headers) res = request.urlopen(req) html = res.read().decode('utf-8') # 保存文件至本地 with open(filename,'w',encoding='utf-8') as f: f.write(html) # 主程序入口 if __name__ == '__main__': word = input('请输入搜索内容:') url = get_url(word) filename = word + '.html' request_url(url,filename)除了使用函数式编程外,您也可以使用面向对象的编程方法(本教程主要以该方法),在后续内容中会做相应介绍。
本文链接:http://task.lmcjl.com/news/13273.html