python3 获取网页内容保存到文件

来源:互联网 发布:网络写手兼职 编辑:程序博客网 时间:2024/05/24 15:37

http://hi.baidu.com/hibaid2083/item/b94ce23e22ca3e09ceb9feeb

import http.client #导入包conn = http.client.HTTPConnection("mlive.info") #连接到地址conn.request("GET", "/index.html") #发送GET请求r1 = conn.getresponse() #获取服务器的响应print(r1.status,r1.reason) #打印服务器返回的状态file = open("c:\\12.txt","w") #待开文件 写方式str = r1.read().decode("utf-8") #读取网页内容,以utf-8方式保存print(str.find("mlive")) #寻找文本file.write(str.replace('\xa0','')) #写到文件并替换 'xa0' 为空字符.     如果不替换掉这个字符,将无法保存.   一个十分恶心的问题.file.close() #关闭文件