-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
214 lines (179 loc) · 8.75 KB
/
scraper.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import scraperwiki
import lxml.html
import sys
import re
#normalized forms for some countries
def createCountriesDict():
countryLookup = dict()
countryLookup['Catalonia'] = 'Spain'
countryLookup['Euskalherria'] = 'Spain'
countryLookup['Galiza'] = 'Spain'
countryLookup['England'] = 'UK'
countryLookup['Northern Ireland'] = 'UK'
countryLookup['Scotland'] = 'UK'
countryLookup['Wales'] = 'UK'
countryLookup['Guadeloupe'] = 'France'
countryLookup['Martinique'] = 'France'
countryLookup['Reunion'] = 'France'
countryLookup['Quebec'] = 'Canada'
countryLookup['USA AB'] = 'USA'
countryLookup['USA Fossil Fuels-East & South'] = 'USA'
countryLookup['USA Fossil Fuels-West & Midwest'] = 'USA'
countryLookup['USA FGH'] = 'USA'
countryLookup['USA ILMNOPQ'] = 'USA'
countryLookup['USA RSTUWXYZ'] = 'USA'
countryLookup['India A-B'] = 'India'
countryLookup['India C-F'] = 'India'
countryLookup['India G-O'] = 'India'
countryLookup['India P-Z'] = 'India'
return countryLookup
def createPreferredTermsDict():
preferredTermsLookup = dict()
preferredTermsLookup['coal'] = 'fuel'
preferredTermsLookup['kind_of_fuel'] = 'fuel'
preferredTermsLookup['kind_of_coal'] = 'fuel'
preferredTermsLookup['origin_of_coal'] = 'origin_of_fuel'
preferredTermsLookup['web_1'] = 'web'
preferredTermsLookup['web_3'] = ''
preferredTermsLookup['wikipedia_1'] = 'wikipedia'
preferredTermsLookup['wikipedia_2'] = 'wikipedia'
preferredTermsLookup['wikipedia1'] = 'wikipedia'
preferredTermsLookup['waikipedia'] = 'wikipedia'
preferredTermsLookup['annual_capacity'] = 'power_capacity'
preferredTermsLookup['annual_net_capacity'] = 'power_capacity'
preferredTermsLookup['annual_net_power_capacity'] = 'power_capacity'
preferredTermsLookup['capacity_installed'] = 'power_capacity'
preferredTermsLookup['installed_net_power_capacity'] = 'power_capacity'
preferredTermsLookup['installed_power_capacity'] = 'power_capacity'
preferredTermsLookup['net_annual_capacity'] = 'power_capacity'
preferredTermsLookup['net_annual_power_capacity'] = 'power_capacity'
preferredTermsLookup['annual_installed_capacity'] = 'installed_capacity'
preferredTermsLookup['storage_capacity'] = 'installed_capacity'
preferredTermsLookup['annual_production'] = 'production'
preferredTermsLookup['products'] = 'production'
preferredTermsLookup['owners'] = 'owner'
preferredTermsLookup['sharehoders'] = 'shareholders'
preferredTermsLookup['shareholers'] = 'shareholders'
preferredTermsLookup['sharehoolders'] = 'shareholders'
preferredTermsLookup['shareholders_i'] = 'shareholders'
preferredTermsLookup['former_name'] = 'other_name'
preferredTermsLookup['other_names'] = 'other_name'
preferredTermsLookup['oter_name'] = 'other_name'
preferredTermsLookup['address_j'] = 'address'
preferredTermsLookup['adress'] = 'address'
preferredTermsLookup['phone'] = 'telephone'
preferredTermsLookup['video'] = ''
return preferredTermsLookup
processedLinks = []
def parsePPLpage(url, table):
try:
pplHtml = scraperwiki.scrape(url)
pplRoot = lxml.html.fromstring(pplHtml)
except:
print sys.exc_info()[0]
print "Can't download " + url
return
installationInfo=dict()
#get PPL name on top of page
pplName = pplRoot.xpath("string(//td[@class='contentheading'])").strip()
installationInfo['name'] = pplName
#get country just below
pplCountry = pplRoot.xpath("//table[@class='contentpaneopen']//span/a/text()")[0]
pplCountry = pplCountry.strip()
if pplCountry in countryLookup:
if pplCountry.find(countryLookup[pplCountry]) < 0:
installationInfo['area'] = pplCountry
pplCountry = countryLookup[pplCountry]
installationInfo['country'] = pplCountry
#get coordinates in javascript code for google maps
coordMatcher = re.search(r"new GLatLng\(\s+([0-9.-]+),\s+([0-9.-]+)\)", pplHtml)
if coordMatcher:
installationInfo['latitude'] = coordMatcher.group(1)
installationInfo['longitude'] = coordMatcher.group(2)
installationInfo['url'] = url
#iterate on bulleted info lines
pplInfos = pplRoot.xpath("//table[@class='contentpaneopen']//li")
for infoLine in pplInfos:
key = infoLine.xpath("string(.//strong)")
if len(key) > 0:
key = key.replace(':','').lower().strip().replace(' ','_')
if key in preferredTermsLookup:
key = preferredTermsLookup[key]
if key.replace('_','').isalnum():
value = infoLine.xpath("string(.//strong/../text()[. != ' '] | .//strong/following-sibling::a/text())")
if len(value) > 0 and not (key in installationInfo):
#clean up some buggy key/value combinations
if key == "power_capacity" and value.find("m3") > 0:
key = "water_capacity"
elif key == "power_capacity" and table.find("power") < 0:
key = "installed_capacity"
elif (key == "web" or key == "web_2") and value.find("wikipedia") > 0:
key = "wikipedia"
elif key == "wikipedia" and value.find("wikipedia") < 0:
key = "web"
if key == "area" and key in installationInfo:
installationInfo[key] += ", " + value.strip()
elif key == "web" or key == "web_2" or key == "wikipedia":
installationInfo[key] = value.strip()
else:
installationInfo[key] = value.replace(':','').strip()
else:
print "invalid key '" + key + "' in " + url
#save to the database
try:
scraperwiki.sqlite.save(unique_keys=['url'], data=installationInfo, table_name=table)
except:
print "Error saving to DB" + ": " + str(sys.exc_info()[1])
def parseCategoryPage(url, table):
targetUrl = url
try:
html = scraperwiki.scrape(targetUrl)
root = lxml.html.fromstring(html)
#handle pagination when there are more than 50 countries for a given fuel type
pagination = root.xpath("//a[@title='Next']/@href")
if len(pagination) > 0:
targetUrl = 'http://www.industryabout.com'+pagination[0].replace('start=50','limit=0')
html = scraperwiki.scrape(targetUrl)
root = lxml.html.fromstring(html)
except:
print sys.exc_info()[0]
print "Can't download " + targetUrl
return
subLinks = []
#this matches on category pages (list of fuels or countries)
if len(root.xpath("//table[@class='contentpane']//td/a[@class='category']")) > 0:
subLinks = root.xpath("//a[@class='category']/@href")
#this matches on intermediate pages that occur for some contries (especially the US)
#those may be linked from several different paths and should be skipped if already processed
elif len(root.xpath("//table[@class='contentpane']//li//a")) > 0:
if table == "powerplants":
subLinks = root.xpath("//table[@class='contentpane']//li//a[contains(.,'Energy') or contains(.,'Power')]/@href")
elif table == "refineries":
subLinks = root.xpath("//table[@class='contentpane']//li//a[contains(.,'Refineries')]/@href")
#this matches on a special page for hydro power in France
elif len(root.xpath("//table[@class='contentpane']//p[contains(.,'Complete list of power stations')]")) > 0:
subLinks = root.xpath("//table[@class='contentpane']//p[contains(.,'Complete list of power stations')]//a/@href")
for link in subLinks:
if link not in processedLinks:
processedLinks.append(link)
parseCategoryPage('http://www.industryabout.com'+link, table)
#this matches on powerplants list
if len(root.xpath("//tr[starts-with(@class,'sectiontableentry')]//a")) > 0:
subLinks = root.xpath("//tr[starts-with(@class,'sectiontableentry')]//a/@href")
for link in subLinks:
if link not in processedLinks:
parsePPLpage('http://www.industryabout.com'+link, table)
processedLinks.append(link)
if len(subLinks) == 0:
print "No links could be found in " + targetUrl
return
countryLookup = createCountriesDict()
preferredTermsLookup = createPreferredTermsDict()
#starting page: energy home page
#home = "http://www.industryabout.com/energy"
#split in chunks to avoid reaching execution time limit
section = scraperwiki.sqlite.get_var('section', 0)
if section == 0:
home = "https://www.industryabout.com/world-fossil-fuels-energy-map"
parseCategoryPage(home, 'powerplants')
scraperwiki.sqlite.save_var('section', 1)