-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathexample_of_usage.py
More file actions
executable file
·36 lines (26 loc) · 919 Bytes
/
example_of_usage.py
File metadata and controls
executable file
·36 lines (26 loc) · 919 Bytes
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
# -----------------------------------------------------------------------------
# Created: 04.03.2014
# Copyright: (c) Josua Schmid 2014
# Licence: AGPLv3
#
# Sample script for parsing HTML tables
# -----------------------------------------------------------------------------
import urllib.request
from pprint import pprint
from html_table_parser import HTMLTableParser
def url_get_contents(url: str) -> bytes:
""" Opens a website and read its binary contents (HTTP Response Body) """
req = urllib.request.Request(url=url)
f = urllib.request.urlopen(req)
return f.read()
def main() -> None:
url = 'https://w3schools.com/html/html_tables.asp'
xhtml = url_get_contents(url).decode('utf-8')
p = HTMLTableParser()
p.feed(xhtml)
# Get all tables
pprint(p.tables)
# Get tables with id attribute
pprint(p.named_tables)
if __name__ == '__main__':
main()