-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add xpath text baseline (without output as it's huge)
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env python3 | ||
import gzip | ||
import json | ||
from pathlib import Path | ||
|
||
import lxml.html | ||
|
||
|
||
def xpath_text(html: str) -> str: | ||
root = lxml.html.fromstring(html) | ||
bodies = root.xpath('//body') | ||
if bodies: | ||
root = bodies[0] | ||
return ' '.join(root.xpath('.//text()')) | ||
|
||
|
||
def main(): | ||
output = {} | ||
for path in Path('html').glob('*.html.gz'): | ||
with gzip.open(path, 'rt', encoding='utf8') as f: | ||
html = f.read() | ||
item_id = path.stem.split('.')[0] | ||
output[item_id] = {'articleBody': xpath_text(html)} | ||
(Path('output') / 'xpath-text.json').write_text( | ||
json.dumps(output, sort_keys=True, ensure_ascii=False, indent=4), | ||
encoding='utf8') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |