-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
59 lines (47 loc) · 1.54 KB
/
server.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
from typing import List
from fastapi import FastAPI
from gne import GeneralNewsExtractor, ListPageExtractor
from pydantic import BaseModel
app = FastAPI()
class ExtractTask(BaseModel):
html: str
title_xpath: str = ''
publish_time_xpath: str = ''
author_xpath: str = ''
noise_node_list: List[str] = []
host: str = ''
with_body_html: bool = False
class ExtractListTask(BaseModel):
sample: str = ''
html: str = ''
@app.get('/')
def index():
return {'success': True}
@app.post('/extract')
def extract(task: ExtractTask):
extractor = GeneralNewsExtractor()
try:
result = extractor.extract(
task.html,
title_xpath= task.title_xpath,
author_xpath=task.author_xpath,
publish_time_xpath=task.publish_time_xpath,
with_body_html=task.with_body_html,
host=task.host,
noise_node_list=task.noise_node_list
)
except Exception as e:
result = {'success': False, 'msg': str(e)}
return result
@app.post('/extract_list')
def extract_list(task: ExtractListTask):
if not task.sample:
return {'success': False, 'msg': '全自动智能提取即将上线,敬请期待。现在请填写列表中任一一项的标题或者 XPath。'}
if not task.html:
return {'success': False, 'msg': '请填写 HTML'}
extractor = ListPageExtractor()
try:
result = extractor.extract(task.html, task.sample)
except Exception as e:
result = {'success': False, 'msg': str(e)}
return result