-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
30 lines (26 loc) · 1018 Bytes
/
parse.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
#!/usr/bin/env python3
from bs4 import BeautifulSoup
from lxml import etree
import argparse
import urllib3
import sys
import code
content = ''.join(sys.argv[1])
bs = BeautifulSoup(content,'lxml')
newest_status = bs.find(lambda tag: tag.name == 'div' \
and tag.has_attr('class') \
and tag.get('class')[0] == 'highlightSRO')
try:
history = bs.find(lambda tag: tag.name == 'table').find_all(lambda tag: tag.name == 'tr')
except Exception as e:
obj_not_found = False
for s in bs.strings:
if 'o possui dados sobre o objeto info' in s:
print('O sistema dos correios não possui dados sobre o objeto informado. ')
obj_not_found = True
sys.exit(1)
if not obj_not_found: print(e)
if None not in [newest_status,history]:
print(newest_status.text.replace('\n\n\n','\n').replace('\n\n','\n'))
[print(h.text.replace('\n\n\n','\n').replace('\n\n','\n')) for h in history] #
else: print('Site dos correios mudou, favor reportar este problema.')