Python:网络工具类

随便开头..

2015.11.1更新:修复一些小问题,具体看源码.

这里的程序仅针对Python3+版本[注意:Python3+和Python2是不兼容的].

不知道多久之前我开始用Python进行网页抓取,前一段时间对程序进行了开源.在这里也贴一下.

python Logo
image-2330

源码及简单说明

这个程序已经放在github上,可以在[这里]找到.

程序里面注释写的比较详细,当然肯定有不完善的地方,欢迎指正[在github上也有一个例子].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from datetime import datetime,timedelta
import time
from enum import Enum, unique
import os
import logging
import urllib3
import codecs
import re
from PIL import  Image

__author__ = 'puruidong'

'''
工具类.


@python version >= 3.4

*:PIL在Python3+中改名为:Pillow[参考:http://pillow.readthedocs.org/en/latest/installation.html#windows-installation]

v1.1 -->
            1.修复写文件无法新增;
            2.修复读取文件未正确返回;
v1.2 -->
            1.修改HTML待转移字符的替换规则[修改后为,所有均转换为空]
            2.新增一个替换HTML标签的方法.
            3.为写文件设置默认编码为utf-8.
v1.3 -->
            1.增加判断文件大小的函数;
            2.增加获取时间戳的函数;
            3.注释及文字简体化(之前由于输入法问题,导致部分文字为繁体.)


@date 2015.6.26 v1.0
@date 2015.8.3 v1.1
@date 2015.8.8 v1.1.0
@date 2015.10.29 v1.3
'''


class FileUtils(object):
    '''创建文件目录,并返回该目录'''
    def mkdir(self,path):
        #去除左右两边的空格
        path = path.strip()
        # 去除尾部 \符号
        path = path.rstrip("\")
        path = self.get_file_path(path)
        if not os.path.exists(path):
            os.makedirs(path)

        return path

    '''获取文件后缀名'''
    def get_file_extension(self,file):
        return os.path.splitext(file)[1]

    '''获取文件的路径'''
    def get_file_path(self,file):
        return os.path.dirname(file)

    '''检查文件是否存在.'''
    def checkfiles(self,path):
        if os.path.exists(path):
            return True
        else:
            return False

    '''获取文件的大小,单位是[kb]'''
    def checkfilesize(self,path):
        if self.checkfiles(path):
            return (os.path.getsize(path))/1024.0
        else:
            return 0


    #获取文件名.
    def get_filename(self,file):
        return os.path.basename(file)

    '''
    #写文件.
    #
    # filepath 文件路径.
    # data 数据.
    # mode 打开文件模式.
    '''
    def writeFile(self,filepath,data,mode="w+",encoding=None):
        with open(filepath,mode,encoding=encoding) as wf:
            wf.write(data)

    '''
    #读文件.
    #
    #filePath 文件路径.
    #mode 打开文件模式.
    #

    '''
    def readFile(self,filePath,mode="r"):
        data=None
        with open(filePath,mode,buffering=-1,encoding=None) as rf:
            data=rf.read()
        return data

#格式化参数枚举
@unique
class FormatDates(Enum):
    YYMMDDHHMMSS="%Y%m%d%H%M%S"
    YYMMDD="%Y%m%d"
    YYMMDDHH="%Y%m%d%H"
    DD="%d"
    YYMM="%Y%m"
    #有格式.
    YY_MM_DD_HH_MM_SS="%Y-%m-%d %H:%M:%S"
    YY_MM_DD="%Y-%m-%d"
    YY_MM_DD_HH="%Y-%m-%d %H"
    YY_MM="%Y-%m"


'''


'''

#无法正常运行!!!
class ImageUtilsPython3(object):
    #根据传入的文件参数,进行缩小化图片处理,待测试.
    def formatImage(self,filepathname,newfilesavepath):
        img=Image.open(filepathname)
        smallimg=img.resize((96,54))
        f=FileUtils()
        f.mkdir(newfilesavepath)
        smallimg.save(newfilesavepath,"JPEG")


class HTMLUtils(object):


    '''
    替换HTML内容里面的HTML标签,注意内容中不能包含<或者>符号.

    使用示例.
    htmlregex=re.compile('<[\s\S]*?>')
    datas='<p class="web_size" id="title-15083947" style="WORD-BREAK: break-all;">发个小时候的!”</p>'
    result=htmlregex.sub('',datas)
    print(result)
    下面的方式也可以替换.
    dr =re.compile(r'<[^>]+>',re.S)
    jsons =re.sub(dr,'',newx)
    '''
    def replaceAllHtmlTag(self,htmlstr):
        htmlregex=re.compile(r'<[^>]+>',re.S)
        result=htmlregex.sub('',htmlstr)
        return result

    #替换html标签.所有待转移的都转换为空.
    def replaceCharEntity(self, htmlstr):
        CHAR_ENTITIES = {'nbsp': ' ', '160': ' ',
                         'lt': '', '60': '',
                         'gt': '', '62': '',
                         'amp': '', '38': '',
                         'quot': '', '34': ''}

        re_charEntity = re.compile('&#?(?P<name>\w+);')
        sz = re_charEntity.search(htmlstr)
        while sz:
            entity = sz.group()  #entity全称,如>
            key = sz.group('name')  #去除&;后entity,如>为gt
            try:
                htmlstr = re_charEntity.sub(CHAR_ENTITIES[key], htmlstr, 1)
                sz = re_charEntity.search(htmlstr)
            except KeyError:
                htmlstr = re_charEntity.sub('', htmlstr, 1)
                sz = re_charEntity.search(htmlstr)
        return htmlstr



#日志
class LogUtils(object):

    '''
    CRITICAL    50
    ERROR   40
    WARNING 30
    INFO    20
    DEBUG   10

    '''

    _logpath=""

    def __init__(self,logpathsfi):
        self._logpath=logpathsfi

    #kw为{"dfilename":xx,"dlineno":xx}
    def logw(self,value,FORMAT="[%(levelname)s]  %(asctime)-15s   %(dfilename)s %(dlineno)s  [message]:%(message)s",LEVEL=20,*,dfilename="NOFILENAME",dlineno=-1):
        value=str(value)
        logging.basicConfig(level=0,filename=self._logpath,format=FORMAT,datefmt="%Y-%m-%d %H:%M:%S")
        a={"dfilename":dfilename,"dlineno":dlineno}
        logging.log(level=LEVEL,msg=value,extra=a)


#获取时间.
class DateUtils(object):

    '''
    获取时间.

    #format 格式化样式。

    --下面的参数设置偏移。(可以用于获取昨天,前天,下個月,下一小時等.)
    #days 天数:>0为增加
                      <0为減少
    #minutes 分钟.用法同上
    #hours 小时.用法同上
    #weeks 周=7天.用法同上

    '''
    def getNowDate(self,format=FormatDates.YYMMDDHHMMSS,days=0, minutes=0, hours=0, weeks=0):
        if isinstance(format,FormatDates):
            return  (datetime.now()+timedelta(days=days,minutes=minutes,hours=hours,weeks=weeks)).strftime(format.value)
        else:
            raise TypeError("格式化类型必须是FormatDates")

    '''
    日期往前推或者往后推!此函数仅能使用日期.

    numbs>0时,日期往后推numbs天.
    numbs<0时,日期往前推numbs天.
    '''
    def getLastDate(self,numbs,format=FormatDates.YYMMDD):
        ns = 1 if numbs>0 else -1
        return [(self.getNowDate(format=FormatDates.YYMMDD,days=i)) for i in range(0,numbs,ns)]

    '''
    返回字符串格式的时间戳.
   
    v: 2015.10.19 修复上一个版本返回的方式.已测可用于其它语言.
    '''
    def getTimestamp(self):
        return str(int(time.mktime(datetime.now().timetuple()))*1000)



#网络操作.
class NetWorkUtils(object):
    ''''''
    #通用抓取网页.
    #method 抓取方式,默认GET.
    #site 站点地址.默认百度.
    #headers 头信息.默认无.
    ''''''
    def network(self,headers=None,methods="GET",site="http://www.baidu.com"):
        http=urllib3.PoolManager()
        r=http.request(methods,site,headers=headers)
        return r.data


    # def testMobileQsbk(self):
class TestDemo(object):
    def logingos(self,numbs=10):
        date=DateUtils()
        # if numbs>0:
        #     rdate = [(date.getNowDate(format=FormatDates.YYMMDD,days=i)) for i in range(0,numbs,1)]
        #     print(rdate)
        #     return
        # elif numbs<0:
        ns = 1 if numbs>0 else -1
        print([(date.getNowDate(format=FormatDates.YYMMDD,days=i)) for i in range(0,numbs,ns)])

    def testInputNt(self):
        try:
            url="http://www.baidu.com"
            n=NetWorkUtils()
            data=n.network(site=url)
            print(data)
            n.writeFile("./test.html",data,mode="w+")
        except Exception as e:
            print("逗比,不要乱输入!")
            raise e


if __name__ == '__main__':
    # t=TestDemo()
    # t.logingos(numbs=100)
   
    strs = "20150502"
    print((datetime.strptime(strs,"%Y%m%d")))

《Python:网络工具类》上有1条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据