-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse_data.py
63 lines (51 loc) · 1.73 KB
/
parse_data.py
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
import json
import os
from dataclasses import dataclass
from requests_html import HTML
from get import SiteFeatureTransformer
from schema import SiteInfoItem
@dataclass
class HTMLRes():
html: HTML
url: str = ''
def recovery_data():
"""
"""
with open('site/src/data.json', 'r') as f:
all_site = json.load(f)
for site in all_site:
site = SiteInfoItem(**site)
with open(f'data/{site.domain}.json', 'w') as f:
site.save_to_file(f)
def parse_data():
"""
添加新的考察特征时,从缓存的 html 中获取数据,更新到站点数据中。
"""
for itemdir in iter(os.listdir('data')):
try:
site_path = 'data/'+itemdir
with open(site_path, 'r') as f:
item = {}
try:
item = json.load(f)
except Exception as e:
print(e)
domain = itemdir[:-5]
html_path = f'html/{domain}.html'
if os.path.exists(html_path):
with open(html_path, 'r') as fhtml:
url = item['url']
html = HTML(url=url, html=fhtml.read())
site_feature = SiteFeatureTransformer(
url=url, r=HTMLRes(url=url, html=html), friends=item['friends'])
site = SiteInfoItem(**site_feature.to_data())
with open(site_path, 'w') as f:
site.save_to_file(f)
else:
print(f'{domain} 的缓存 html 文件不存在')
except Exception as e:
print(e)
print(f"打开 {itemdir} 失败")
if __name__ == "__main__":
# recovery_data()
parse_data()