forked from Boris-code/feapder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
2290 lines (1826 loc) · 59.1 KB
/
tools.py
File metadata and controls
2290 lines (1826 loc) · 59.1 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on 2018-09-06 14:21
---------
@summary: 工具
---------
@author: Boris
@email: boris@bzkj.tech
"""
import calendar
import codecs
import configparser # 读配置文件的
import datetime
import functools
import hashlib
import html
import json
import os
import pickle
import random
import re
import socket
import ssl
import string
import sys
import time
import traceback
import urllib
import urllib.parse
import uuid
import weakref
from hashlib import md5
from pprint import pformat
from pprint import pprint
from urllib import request
from urllib.parse import urljoin
import execjs # pip install PyExecJS
import redis
import requests
import six
from requests.cookies import RequestsCookieJar
from w3lib.url import canonicalize_url as _canonicalize_url
import feapder.setting as setting
from feapder.utils.email_sender import EmailSender
from feapder.utils.log import log
os.environ["EXECJS_RUNTIME"] = "Node" # 设置使用node执行js
# 全局取消ssl证书验证
ssl._create_default_https_context = ssl._create_unverified_context
TIME_OUT = 30
TIMER_TIME = 5
redisdb = None
def get_redisdb():
global redisdb
if not redisdb:
ip, port = setting.REDISDB_IP_PORTS.split(":")
redisdb = redis.Redis(
host=ip,
port=port,
db=setting.REDISDB_DB,
password=setting.REDISDB_USER_PASS,
decode_responses=True,
) # redis默认端口是6379
return redisdb
# 装饰器
class Singleton(object):
def __init__(self, cls):
self._cls = cls
self._instance = {}
def __call__(self, *args, **kwargs):
if self._cls not in self._instance:
self._instance[self._cls] = self._cls(*args, **kwargs)
return self._instance[self._cls]
def log_function_time(func):
try:
@functools.wraps(func) # 将函数的原来属性付给新函数
def calculate_time(*args, **kw):
began_time = time.time()
callfunc = func(*args, **kw)
end_time = time.time()
log.debug(func.__name__ + " run time = " + str(end_time - began_time))
return callfunc
return calculate_time
except:
log.debug("求取时间无效 因为函数参数不符")
return func
def run_safe_model(module_name):
def inner_run_safe_model(func):
try:
@functools.wraps(func) # 将函数的原来属性付给新函数
def run_func(*args, **kw):
callfunc = None
try:
callfunc = func(*args, **kw)
except Exception as e:
log.error(module_name + ": " + func.__name__ + " - " + str(e))
traceback.print_exc()
return callfunc
return run_func
except Exception as e:
log.error(module_name + ": " + func.__name__ + " - " + str(e))
traceback.print_exc()
return func
return inner_run_safe_model
def memoizemethod_noargs(method):
"""Decorator to cache the result of a method (without arguments) using a
weak reference to its object
"""
cache = weakref.WeakKeyDictionary()
@functools.wraps(method)
def new_method(self, *args, **kwargs):
if self not in cache:
cache[self] = method(self, *args, **kwargs)
return cache[self]
return new_method
########################【网页解析相关】###############################
# @log_function_time
def get_html_by_requests(
url, headers=None, code="utf-8", data=None, proxies={}, with_response=False
):
html = ""
r = None
try:
if data:
r = requests.post(
url, headers=headers, timeout=TIME_OUT, data=data, proxies=proxies
)
else:
r = requests.get(url, headers=headers, timeout=TIME_OUT, proxies=proxies)
if code:
r.encoding = code
html = r.text
except Exception as e:
log.error(e)
finally:
r and r.close()
if with_response:
return html, r
else:
return html
def get_json_by_requests(
url,
params=None,
headers=None,
data=None,
proxies={},
with_response=False,
cookies=None,
):
json = {}
response = None
try:
# response = requests.get(url, params = params)
if data:
response = requests.post(
url,
headers=headers,
data=data,
params=params,
timeout=TIME_OUT,
proxies=proxies,
cookies=cookies,
)
else:
response = requests.get(
url,
headers=headers,
params=params,
timeout=TIME_OUT,
proxies=proxies,
cookies=cookies,
)
response.encoding = "utf-8"
json = response.json()
except Exception as e:
log.error(e)
finally:
response and response.close()
if with_response:
return json, response
else:
return json
def get_cookies(response):
cookies = requests.utils.dict_from_cookiejar(response.cookies)
return cookies
def get_cookies_jar(cookies):
"""
@summary: 适用于selenium生成的cookies转requests的cookies
requests.get(xxx, cookies=jar)
参考:https://www.cnblogs.com/small-bud/p/9064674.html
---------
@param cookies: [{},{}]
---------
@result: cookie jar
"""
cookie_jar = RequestsCookieJar()
for cookie in cookies:
cookie_jar.set(cookie["name"], cookie["value"])
return cookie_jar
def get_cookies_from_selenium_cookie(cookies):
"""
@summary: 适用于selenium生成的cookies转requests的cookies
requests.get(xxx, cookies=jar)
参考:https://www.cnblogs.com/small-bud/p/9064674.html
---------
@param cookies: [{},{}]
---------
@result: cookie jar
"""
cookie_dict = {}
for cookie in cookies:
if cookie.get("name"):
cookie_dict[cookie["name"]] = cookie["value"]
return cookie_dict
def cookiesjar2str(cookies):
str_cookie = ""
for k, v in requests.utils.dict_from_cookiejar(cookies).items():
str_cookie += k
str_cookie += "="
str_cookie += v
str_cookie += "; "
return str_cookie
def cookies2str(cookies):
str_cookie = ""
for k, v in cookies.items():
str_cookie += k
str_cookie += "="
str_cookie += v
str_cookie += "; "
return str_cookie
def get_urls(
html,
stop_urls=(
"javascript",
"+",
".css",
".js",
".rar",
".xls",
".exe",
".apk",
".doc",
".jpg",
".png",
".flv",
".mp4",
),
):
# 不匹配javascript、 +、 # 这样的url
regex = r'<a.*?href.*?=.*?["|\'](.*?)["|\']'
urls = get_info(html, regex)
urls = sorted(set(urls), key=urls.index)
if stop_urls:
stop_urls = isinstance(stop_urls, str) and [stop_urls] or stop_urls
use_urls = []
for url in urls:
for stop_url in stop_urls:
if stop_url in url:
break
else:
use_urls.append(url)
urls = use_urls
return urls
def get_full_url(root_url, sub_url):
"""
@summary: 得到完整的ur
---------
@param root_url: 根url (网页的url)
@param sub_url: 子url (带有相对路径的 可以拼接成完整的)
---------
@result: 返回完整的url
"""
return urljoin(root_url, sub_url)
def joint_url(url, params):
# param_str = "?"
# for key, value in params.items():
# value = isinstance(value, str) and value or str(value)
# param_str += key + "=" + value + "&"
#
# return url + param_str[:-1]
if not params:
return url
params = urlencode(params)
separator = "?" if "?" not in url else "&"
return url + separator + params
def canonicalize_url(url):
"""
url 归一化 会参数排序 及去掉锚点
"""
return _canonicalize_url(url)
def get_url_md5(url):
url = canonicalize_url(url)
url = re.sub("^http://", "https://", url)
return get_md5(url)
def fit_url(urls, identis):
identis = isinstance(identis, str) and [identis] or identis
fit_urls = []
for link in urls:
for identi in identis:
if identi in link:
fit_urls.append(link)
return list(set(fit_urls))
def get_param(url, key):
params = url.split("?")[-1].split("&")
for param in params:
key_value = param.split("=", 1)
if key == key_value[0]:
return key_value[1]
return None
def urlencode(params):
"""
字典类型的参数转为字符串
@param params:
{
'a': 1,
'b': 2
}
@return: a=1&b=2
"""
return urllib.parse.urlencode(params)
def urldecode(url):
"""
将字符串类型的参数转为json
@param url: xxx?a=1&b=2
@return:
{
'a': 1,
'b': 2
}
"""
params_json = {}
params = url.split("?")[-1].split("&")
for param in params:
key, value = param.split("=")
params_json[key] = unquote_url(value)
return params_json
def unquote_url(url, encoding="utf-8"):
"""
@summary: 将url解码
---------
@param url:
---------
@result:
"""
return urllib.parse.unquote(url, encoding=encoding)
def quote_url(url, encoding="utf-8"):
"""
@summary: 将url编码 编码意思http://www.w3school.com.cn/tags/html_ref_urlencode.html
---------
@param url:
---------
@result:
"""
return urllib.parse.quote(url, safe="%;/?:@&=+$,", encoding=encoding)
def quote_chinese_word(text, encoding="utf-8"):
def quote_chinese_word_func(text):
chinese_word = text.group(0)
return urllib.parse.quote(chinese_word, encoding=encoding)
return re.sub("([\u4e00-\u9fa5]+)", quote_chinese_word_func, text, flags=re.S)
def unescape(str):
"""
反转译
"""
return html.unescape(str)
def excape(str):
"""
转译
"""
return html.escape(str)
_regexs = {}
# @log_function_time
def get_info(html, regexs, allow_repeat=True, fetch_one=False, split=None):
regexs = isinstance(regexs, str) and [regexs] or regexs
infos = []
for regex in regexs:
if regex == "":
continue
if regex not in _regexs.keys():
_regexs[regex] = re.compile(regex, re.S)
if fetch_one:
infos = _regexs[regex].search(html)
if infos:
infos = infos.groups()
else:
continue
else:
infos = _regexs[regex].findall(str(html))
if len(infos) > 0:
# print(regex)
break
if fetch_one:
infos = infos if infos else ("",)
return infos if len(infos) > 1 else infos[0]
else:
infos = allow_repeat and infos or sorted(set(infos), key=infos.index)
infos = split.join(infos) if split else infos
return infos
def table_json(table, save_one_blank=True):
"""
将表格转为json 适应于 key:value 在一行类的表格
@param table: 使用selector封装后的具有xpath的selector
@param save_one_blank: 保留一个空白符
@return:
"""
data = {}
trs = table.xpath(".//tr")
for tr in trs:
tds = tr.xpath("./td|./th")
for i in range(0, len(tds), 2):
if i + 1 > len(tds) - 1:
break
key = tds[i].xpath("string(.)").extract_first(default="").strip()
value = tds[i + 1].xpath("string(.)").extract_first(default="").strip()
value = replace_str(value, "[\f\n\r\t\v]", "")
value = replace_str(value, " +", " " if save_one_blank else "")
if key:
data[key] = value
return data
def get_table_row_data(table):
"""
获取表格里每一行数据
@param table: 使用selector封装后的具有xpath的selector
@return: [[],[]..]
"""
datas = []
rows = table.xpath(".//tr")
for row in rows:
cols = row.xpath("./td|./th")
row_datas = []
for col in cols:
data = col.xpath("string(.)").extract_first(default="").strip()
row_datas.append(data)
datas.append(row_datas)
return datas
def rows2json(rows, keys=None):
"""
将行数据转为json
@param rows: 每一行的数据
@param keys: json的key,空时将rows的第一行作为key
@return:
"""
data_start_pos = 0 if keys else 1
datas = []
keys = keys or rows[0]
for values in rows[data_start_pos:]:
datas.append(dict(zip(keys, values)))
return datas
def get_form_data(form):
"""
提取form中提交的数据
:param form: 使用selector封装后的具有xpath的selector
:return:
"""
data = {}
inputs = form.xpath(".//input")
for input in inputs:
name = input.xpath("./@name").extract_first()
value = input.xpath("./@value").extract_first()
if name:
data[name] = value
return data
# mac上不好使
# def get_domain(url):
# domain = ''
# try:
# domain = get_tld(url)
# except Exception as e:
# log.debug(e)
# return domain
def get_domain(url):
proto, rest = urllib.parse.splittype(url)
domain, rest = urllib.parse.splithost(rest)
return domain
def get_index_url(url):
return "/".join(url.split("/")[:3])
def get_ip(domain):
ip = socket.getaddrinfo(domain, "http")[0][4][0]
return ip
def get_localhost_ip():
"""
利用 UDP 协议来实现的,生成一个UDP包,把自己的 IP 放如到 UDP 协议头中,然后从UDP包中获取本机的IP。
这个方法并不会真实的向外部发包,所以用抓包工具是看不到的
:return:
"""
s = None
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
finally:
if s:
s.close()
return ip
def ip_to_num(ip):
import struct
ip_num = socket.ntohl(struct.unpack("I", socket.inet_aton(str(ip)))[0])
return ip_num
def is_valid_proxy(proxy, check_url=None):
"""
检验代理是否有效
@param proxy: xxx.xxx.xxx:xxx
@param check_url: 利用目标网站检查,目标网站url。默认为None, 使用代理服务器的socket检查, 但不能排除Connection closed by foreign host
@return: True / False
"""
is_valid = False
if check_url:
proxies = {"http": f"http://{proxy}", "https": f"https://{proxy}"}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
}
response = None
try:
response = requests.get(
check_url, headers=headers, proxies=proxies, stream=True, timeout=20
)
is_valid = True
except Exception as e:
log.error("check proxy failed: {} {}".format(e, proxy))
finally:
if response:
response.close()
else:
ip, port = proxy.split(":")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sk:
sk.settimeout(7)
try:
sk.connect((ip, int(port))) # 检查代理服务器是否开着
is_valid = True
except Exception as e:
log.error("check proxy failed: {} {}:{}".format(e, ip, port))
return is_valid
def is_valid_url(url):
"""
验证url是否合法
:param url:
:return:
"""
if re.match(r"(^https?:/{2}\w.+$)|(ftp://)", url):
return True
else:
return False
def get_text(soup, *args):
try:
return soup.get_text()
except Exception as e:
log.error(e)
return ""
def del_html_tag(content, except_line_break=False, save_img=False, white_replaced=""):
"""
删除html标签
@param content: html内容
@param except_line_break: 保留p标签
@param save_img: 保留图片
@param white_replaced: 空白符替换
@return:
"""
content = replace_str(content, "(?i)<script(.|\n)*?</script>") # (?)忽略大小写
content = replace_str(content, "(?i)<style(.|\n)*?</style>")
content = replace_str(content, "<!--(.|\n)*?-->")
content = replace_str(
content, "(?!&[a-z]+=)&[a-z]+;?"
) # 干掉 等无用的字符 但&xxx= 这种表示参数的除外
if except_line_break:
content = content.replace("</p>", "/p")
content = replace_str(content, "<[^p].*?>")
content = content.replace("/p", "</p>")
content = replace_str(content, "[ \f\r\t\v]")
elif save_img:
content = replace_str(content, "(?!<img.+?>)<.+?>") # 替换掉除图片外的其他标签
content = replace_str(content, "(?! +)\s+", "\n") # 保留空格
content = content.strip()
else:
content = replace_str(content, "<(.|\n)*?>")
content = replace_str(content, "\s", white_replaced)
content = content.strip()
return content
def del_html_js_css(content):
content = replace_str(content, "(?i)<script(.|\n)*?</script>") # (?)忽略大小写
content = replace_str(content, "(?i)<style(.|\n)*?</style>")
content = replace_str(content, "<!--(.|\n)*?-->")
return content
def is_have_chinese(content):
regex = "[\u4e00-\u9fa5]+"
chinese_word = get_info(content, regex)
return chinese_word and True or False
def is_have_english(content):
regex = "[a-zA-Z]+"
english_words = get_info(content, regex)
return english_words and True or False
def get_chinese_word(content):
regex = "[\u4e00-\u9fa5]+"
chinese_word = get_info(content, regex)
return chinese_word
def get_english_words(content):
regex = "[a-zA-Z]+"
english_words = get_info(content, regex)
return english_words or ""
##################################################
def get_json(json_str):
"""
@summary: 取json对象
---------
@param json_str: json格式的字符串
---------
@result: 返回json对象
"""
try:
return json.loads(json_str) if json_str else {}
except Exception as e1:
try:
json_str = json_str.strip()
json_str = json_str.replace("'", '"')
keys = get_info(json_str, "(\w+):")
for key in keys:
json_str = json_str.replace(key, '"%s"' % key)
return json.loads(json_str) if json_str else {}
except Exception as e2:
log.error(
"""
e1: %s
format json_str: %s
e2: %s
"""
% (e1, json_str, e2)
)
return {}
def jsonp2json(jsonp):
"""
将jsonp转为json
@param jsonp: jQuery172013600082560040794_1553230569815({})
@return:
"""
try:
return json.loads(re.match(".*?({.*}).*", jsonp, re.S).group(1))
except:
raise ValueError("Invalid Input")
def dumps_json(json_, indent=4, sort_keys=False):
"""
@summary: 格式化json 用于打印
---------
@param json_: json格式的字符串或json对象
---------
@result: 格式化后的字符串
"""
try:
if isinstance(json_, str):
json_ = get_json(json_)
json_ = json.dumps(
json_, ensure_ascii=False, indent=indent, skipkeys=True, sort_keys=sort_keys
)
except Exception as e:
log.error(e)
json_ = pformat(json_)
return json_
def get_json_value(json_object, key):
"""
@summary:
---------
@param json_object: json对象或json格式的字符串
@param key: 建值 如果在多个层级目录下 可写 key1.key2 如{'key1':{'key2':3}}
---------
@result: 返回对应的值,如果没有,返回''
"""
current_key = ""
value = ""
try:
json_object = (
isinstance(json_object, str) and get_json(json_object) or json_object
)
current_key = key.split(".")[0]
value = json_object[current_key]
key = key[key.find(".") + 1 :]
except Exception as e:
return value
if key == current_key:
return value
else:
return get_json_value(value, key)
def get_all_keys(datas, depth=None, current_depth=0):
"""
@summary: 获取json李所有的key
---------
@param datas: dict / list
@param depth: 字典key的层级 默认不限制层级 层级从1开始
@param current_depth: 字典key的当前层级 不用传参
---------
@result: 返回json所有的key
"""
keys = []
if depth and current_depth >= depth:
return keys
if isinstance(datas, list):
for data in datas:
keys.extend(get_all_keys(data, depth, current_depth=current_depth + 1))
elif isinstance(datas, dict):
for key, value in datas.items():
keys.append(key)
if isinstance(value, dict):
keys.extend(get_all_keys(value, depth, current_depth=current_depth + 1))
return keys
def to_chinese(unicode_str):
format_str = json.loads('{"chinese":"%s"}' % unicode_str)
return format_str["chinese"]
##################################################
def replace_str(source_str, regex, replace_str=""):
"""
@summary: 替换字符串
---------
@param source_str: 原字符串
@param regex: 正则
@param replace_str: 用什么来替换 默认为''
---------
@result: 返回替换后的字符串
"""
str_info = re.compile(regex)
return str_info.sub(replace_str, source_str)
def del_redundant_blank_character(text):
"""
删除冗余的空白符, 只保留一个
:param text:
:return:
"""
return re.sub("\s+", " ", text)
##################################################
def get_conf_value(config_file, section, key):
cp = configparser.ConfigParser(allow_no_value=True)
with codecs.open(config_file, "r", encoding="utf-8") as f:
cp.read_file(f)
return cp.get(section, key)
def mkdir(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
pass
def write_file(filename, content, mode="w", encoding="utf-8"):
"""
@summary: 写文件
---------
@param filename: 文件名(有路径)
@param content: 内容
@param mode: 模式 w/w+ (覆盖/追加)
---------
@result:
"""
directory = os.path.dirname(filename)
mkdir(directory)
with open(filename, mode, encoding=encoding) as file:
file.writelines(content)
def read_file(filename, readlines=False, encoding="utf-8"):
"""
@summary: 读文件
---------
@param filename: 文件名(有路径)
@param readlines: 按行读取 (默认False)
---------
@result: 按行读取返回List,否则返回字符串
"""
content = None
try:
with open(filename, "r", encoding=encoding) as file:
content = file.readlines() if readlines else file.read()
except Exception as e:
log.error(e)
return content
def get_oss_file_list(oss_handler, prefix, date_range_min, date_range_max=None):
"""
获取文件列表
@param prefix: 路径前缀 如 data/car_service_line/yiche/yiche_serial_zongshu_info
@param date_range_min: 时间范围 最小值 日期分隔符为/ 如 2019/03/01 或 2019/03/01/00/00/00
@param date_range_max: 时间范围 最大值 日期分隔符为/ 如 2019/03/01 或 2019/03/01/00/00/00
@return: 每个文件路径 如 html/e_commerce_service_line/alibaba/alibaba_shop_info/2019/03/22/15/53/15/8ca8b9e4-4c77-11e9-9dee-acde48001122.json.snappy
"""
# 计算时间范围
date_range_max = date_range_max or date_range_min
date_format = "/".join(
["%Y", "%m", "%d", "%H", "%M", "%S"][: date_range_min.count("/") + 1]
)
time_interval = [
{"days": 365},
{"days": 31},
{"days": 1},
{"hours": 1},
{"minutes": 1},
{"seconds": 1},
][date_range_min.count("/")]
date_range = get_between_date(
date_range_min, date_range_max, date_format=date_format, **time_interval
)
for date in date_range:
file_folder_path = os.path.join(prefix, date)
objs = oss_handler.list(prefix=file_folder_path)
for obj in objs:
filename = obj.key
yield filename
def is_html(url):
if not url:
return False
try:
content_type = request.urlopen(url).info().get("Content-Type", "")