import chardet
import urllib.request
html = urllib.request.urlopen('http://www.baidu.com/').read()
print (chardet.detect(html))
C:\python\python.exe D:/python/test/2.py
{'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}
Process finished with exit code 0
import urllib.request
import os
from bs4 import BeautifulSoup
import time
def getHtml(url):
html = urllib.request.urlopen(url).read()
return html
#获取字符串,因为我在国外,路由器low到爆,编码格式也蛋疼了我很久
html = getHtml("http://192.168.0.254/pub/fbx_info.txt")
#将read()获取的是bytes编码转化成str
html = html.decode("ISO-8859-1")
#操作字符串
html = html.split('WAN')[1].split('Ethernet')[0]
time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
#因为使用记事本,在记事本中换行只认\r\n
str = time+html+"\r\n"
# 保存文件为txt,win环境路径
outfile = open("C:\\Users\\sw\\Desktop\\route.txt","a+",encoding="utf-8",newline='\n')
outfile.write(str)
outfile.close()
print("文件已保存到本地")
import urllib.request
import os
from bs4 import BeautifulSoup
import time
def getHtml(url):
html = urllib.request.urlopen(url).read()
return html
#获取字符串,因为我在国外,路由器low到爆,编码格式也蛋疼了我很久
html = getHtml("http://192.168.0.254/pub/fbx_info.txt")
#将read()获取的是bytes编码转化成str
html = html.decode("ISO-8859-1")
#再次检测编码
#print(type(html))
#操作字符串
html = html.split('WAN')[1].split('Ethernet')[0]
time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
#因为使用记事本,在记事本中换行只认\r\n
str = time+html+"\r\n"
# 保存文件为txt,win环境路径
#print(type(str))
outfile = open("C:\\Users\\sw\\Desktop\\route.txt","a+",encoding="utf-8",newline='\n')
outfile.write(str)
outfile.close()
print("文件已保存到本地")
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有