/
scraper.py
189 lines (149 loc) · 5.63 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
# This is a template for a Python scraper on Morph (https://morph.io)
# including some code snippets below that you should find helpful
# import scraperwiki
# import lxml.html
#
# # Read in a page
# html = scraperwiki.scrape("http://foo.com")
#
# # Find something on the page using css selectors
# root = lxml.html.fromstring(html)
# root.cssselect("div[align='left']")
#
# # Write out to the sqlite database using scraperwiki library
# scraperwiki.sqlite.save(unique_keys=['name'], data={"name": "susan", "occupation": "software developer"})
#
# # An arbitrary query against the database
# scraperwiki.sql.select("* from data where 'name'='peter'")
# You don't have to do things with the ScraperWiki and lxml libraries. You can use whatever libraries are installed
# on Morph for Python (https://github.com/openaustralia/morph-docker-python/blob/master/pip_requirements.txt) and all that matters
# is that your final data is written to an Sqlite database called data.sqlite in the current working directory which
# has at least a table called data.
#!/usr/bin/env python
import scraperwiki
import requests
import lxml.html
scraperwiki.sqlite.execute("drop table if exists swdata")
#html = requests.get("http://www.who.int/csr/don/archive/disease/ebola/en/")
#print html.content
url = "http://www.who.int/csr/don/archive/disease/ebola/en/"
root = lxml.html.parse(url).getroot()
#print root
div = root.xpath( '//div[@class="col_2-1_1"]' )
#print div
urllist = []
rownum = 0
for row in div:
rownum = rownum + 1
# #print row
# cellnum = 0
# datacell = 0
#print row.text_content()
for cell in row.xpath('//li/a/@href'):
#if rownum == 1:
#print cell.text_content()
#print str(rownum)
if all( x in cell for x in ["2014","_ebola/"] ):
#if "_ebola/" in cell:
#print cell
urlstr = "http://www.who.int" + cell
#print urlstr
urllist.append(urlstr)
#print colnum
#print cell.text_content()
for url in urllist:
print url
root = lxml.html.parse(url).getroot()
docstr = lxml.html.tostring(root)
recno = 0
div = root.xpath( '//div[@id="main"]' )
colnum = 0
rownum = 0
for row in div:
rownum = rownum + 1
cellnum = 0
datacell = 0
for cell in row.xpath('//td'):
#if rownum == 1:
#print cell.text_content()
cellnum = cellnum + 1
colnum = colnum + 1
#print str(cellnum % 6)
#print str(cellnum) + " " + cell.text_content()
#scraperwiki.sqlite.save(unique_keys=['recno'],data={"recno":cellnum,"country":"","new":""})
if cellnum > 6:
datacell = datacell + 1
if (datacell % 18) == 1:
country = cell.text_content()
if (datacell % 18) == 8:
new = cell.text_content()
if (datacell % 18) == 9:
confirmed = cell.text_content()
if (datacell % 18) == 10:
probable = cell.text_content()
if (datacell % 18) == 11:
suspect = cell.text_content()
if (datacell % 18) == 12:
total = cell.text_content()
if (datacell % 18) == 0:
print country
#scraperwiki.sqlite.save(unique_keys=['recno'],data={"recno":cellnum,"country":country,"new":new,"confirmed":confirmed,"probable":probable,"suspect":suspect,"total":total})
#print "rows = " + str(rownum)
#print "cells = " + str(cellnum)
#//*[@id="primary"]/table/tbody/tr[2]/td[1]
# Saving data:
# unique_keys = [ 'id' ]
# data = { 'id':12, 'name':'violet', 'age':7 }
# scraperwiki.sql.save(unique_keys, data)
#print "rows = " + str(rownum)
#url = "http://www.who.int/csr/don/2014_07_31_ebola/en/"
#root = lxml.html.parse(url).getroot()
#print root
#docstr = lxml.html.tostring(root)
##print docstr
#recno = 0
#div = root.xpath( '//div[@id="main"]' )
#print div
#colnum = 0
#rownum = 0
#for row in div:
# rownum = rownum + 1
# #print row
# cellnum = 0
# datacell = 0
# for cell in row.xpath('//td'):
# #if rownum == 1:
# #print cell.text_content()
#
# cellnum = cellnum + 1
# colnum = colnum + 1
# #print colnum
# #print cell.text_content()
# #print cell.attrib['class']
# print str(cellnum % 6)
# #print str(cellnum) + " " + cell.text_content()
#
# #scraperwiki.sqlite.save(unique_keys=['recno'],data={"recno":cellnum,"country":"","new":""})
# if cellnum > 6:
# datacell = datacell + 1
# if (datacell % 18) == 1:
# country = cell.text_content()
# if (datacell % 18) == 8:
# new = cell.text_content()
# if (datacell % 18) == 9:
# confirmed = cell.text_content()
# if (datacell % 18) == 10:
# probable = cell.text_content()
# if (datacell % 18) == 11:
# suspect = cell.text_content()
# if (datacell % 18) == 12:
# total = cell.text_content()
# if (datacell % 18) == 0:
# scraperwiki.sqlite.save(unique_keys=['recno'],data={"recno":cellnum,"country":country,"new":new,"confirmed":confirmed,"probable":probable,"suspect":suspect,"total":total})
#print "rows = " + str(rownum)
#print "cells = " + str(cellnum)
#//*[@id="primary"]/table/tbody/tr[2]/td[1]
# Saving data:
# unique_keys = [ 'id' ]
# data = { 'id':12, 'name':'violet', 'age':7 }
# scraperwiki.sql.save(unique_keys, data)