import requests def load_page(url): headers={"User-Agent":"Mozilla/5.0(compatible;MSIE 9.0;Windows NT 6.1;Trident/5.0,"} request=requests.get(url,headers=headers) return request.text def save_file(html,filename): print("正在保存:",filename) with open(filename,'w',encoding='utf-8') as file: file.write(html) baidupage=load_page("https://www.baidu.com") save_file(baidupage,"baidu.html") path='baidu.html' with open(path, 'r', encoding='utf-8') as file: html_content=file.read() print(html_content)
with open('a.txt','w',encoding='utf-8') as f: f.write('爬虫,你好') f.write(("\n===================\n")) f.writelines(['等等,爬虫']) f.write(("\n===================\n")) f.writelines(['爬虫开发','样例测试']) with open('a.txt','r',encoding='utf-8') as f: content=f.read() print(content)