-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcal_fires.py
66 lines (60 loc) · 2.09 KB
/
cal_fires.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
60
61
62
63
64
65
66
from disaster_scrapers import Scraper
from bs4 import BeautifulSoup as Soup
import requests
class FireScraper(Scraper):
owner = "simonw"
repo = "disaster-data"
def __init__(self, token, incident_id, name):
self.incident_id = incident_id
self.filepath = "cal-fires/{}-{}.json".format(
self.incident_id, name.lower().strip().replace(" ", "-")
)
super().__init__(token)
def fetch_data(self):
soup = Soup(
requests.get(
"http://www.fire.ca.gov/current_incidents/incidentdetails/Index/{}".format(
self.incident_id
)
).content,
"html5lib",
)
table = soup.select(".incident_table")[0]
info = {"summary": table["summary"]}
for tr in table.select("tr"):
try:
title = tr.select("td.emphasized")[0]
info[title.text.strip().rstrip(":")] = title.find_next(
"td"
).text.strip()
except:
pass
# Split out latitude and longitude, if available
if "Long/Lat" in info:
longitude, latitude = info["Long/Lat"].split("/")
info["latitude"] = latitude
info["longitude"] = longitude
return info
def load_scrapers(token):
# Scrape the list of fires
url = "http://www.fire.ca.gov/current_incidents/"
page = 1
incidents = [] # List of (id, name) tuples
while True:
html = requests.get("{}?page={}".format(url, page)).text
soup = Soup(html, "html5lib")
scraped_incidents = [
(t.get("id"), t.get("title"))
for t in soup.select("table.incident_table")
if t.get("id")
]
if not scraped_incidents:
break
incidents.extend(scraped_incidents)
page += 1
if "?page={}".format(page) not in html:
break
if page > 100:
break
# Return a scraper for each incident
return [FireScraper(token, incident_id, name) for incident_id, name in incidents]