blob: d1599c4fd40e35a8acbefb6802ea19ac737de8cc (
plain)
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
64
65
66
67
68
69
70
71
72
73
74
|
#!/usr/bin/env python3
"""Print earthquakes near a given location.
- i should be able to query for time since, like "show me earthquakes near me
for the last 2 months"
this was all copied, i need to redo it:
"""
from bs4 import BeautifulSoup
import requests
from collections import OrderedDict
"""
returns a list of list
at index =0, there is header
rest lists are real data
"""
def get_recent(url):
result = []
response = requests.get(url)
if response.status_code != 200:
return []
extractor = BeautifulSoup(response.content, "html.parser")
# content = extractor.find_all("div", {'class': 'block2-content'})[0]
content = extractor.find_all("div", {"class": "data_table_wrapper"})[0]
table = content.find_all(["tr"])
# table header shit
table_header = table[0]
header_list = [data.get_text() for data in table_header("td")][:-1]
result.append(header_list)
"""
table = table[1::]
for row in table:
table_data = [ td.get_text() for td in row.find_all('td') if not td('form')]
data = OrderedDict()
for index, val in enumerate(table_data):
data[header_list[index]] = val
result.append(data)
"""
table = table[1::]
for row in table:
table_data = [td.get_text() for td in row.find_all("td") if not td("form")]
result.append(table_data)
return result
def print_table(table):
table = table[1::]
for row in table:
for data in row:
print("{:15s}".format(data), end="")
print("\n")
def main():
url = "http://www.seismonepal.gov.np/"
print("source: {}".format(url))
print("-" * 100)
earthquake = get_recent(url)
print_table(earthquake)
if __name__ == "__main__":
main()
|