-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscrape_images_of_year.py
149 lines (112 loc) · 4.39 KB
/
scrape_images_of_year.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
import numpy
from lxml import html
import requests
import re
import urllib
import os
import sys
import requests
import json
from requests.exceptions import ConnectionError
import time
def download_images(show_observations_list,wanted_year):
gbif_information_d = {}
dictionary_file = open('observations_'+str(wanted_year)+'.json','a')
dictionary_file.write('[')
try:
for ob in show_observations_list:
print 'observation_number: '+str(ob)
show_observations_url = 'http://mushroomobserver.org'+ob
page_observation=None
try:
page_observation = requests.get(show_observations_url)
except ConnectionError:
time.sleep(10)
page_observation = requests.get(show_observations_url)
tree_observation = html.fromstring(page_observation.content)
name = tree_observation.xpath('//span[@id="title-caption"]//i/text()')
if len(name)>0:
date = tree_observation.xpath('//div[@id="observation_when"]/strong/text()')[0]
date_split = date.split('-')
year = date_split[0]
month = int(date_split[1])
day = int(date_split[2])
print 'date: '+str(date)
if int(year)<int(wanted_year):
pass
elif int(year)>int(wanted_year):
pass
else:
name = name[0]
name_id = tree_observation.xpath('//div[@class="list-group"]//div[@class="small"]/a/@href')[0]
name_page=""
try:
name_page = requests.get('http://mushroomobserver.org'+name_id).content
except ConnectionError:
time.sleep(10)
name_page = requests.get('http://mushroomobserver.org'+name_id).content
if 'Preferred Synonym(s)' in name_page:
tree_names = html.fromstring(name_page.split('Preferred Synonym(s):')[1].split('</a>')[0])
preffered_name = tree_names.xpath('//a/b/i/text()')[0]
name = preffered_name
name = name.replace('"','')
images = tree_observation.xpath('//div[@class="show_images list-group"]//img/@alt')
where = tree_observation.xpath('//div[@id="observation_where"]//a/@href')[0]
who = tree_observation.xpath('//div[@id="observation_who"]//a/@href')[0]
gbif_response = None
if name in gbif_information_d:
gbif_response = gbif_information_d[name]
else:
url = 'http://api.gbif.org/v1/species/match?name='+name
try:
gbif_response = requests.get(url).json()
gbif_information_d[name] = gbif_response
except:
print 'not able to get gbif information '+name
if len(images)>0:
for i in range(len(images)):
d = {}
image = images[i]
d['label'] = name
d['date'] = date
d['image_id'] = image
d['image_url'] = 'http://mushroomobserver.org/images/320/' + image
d['location'] = where
d['user'] = who
d['gbif_info'] = gbif_response
d['observation'] = ob
if i==0:
d['thumbnail']=1
else:
d['thumbnail']=0
json.dump(d,dictionary_file)
dictionary_file.write(',')
except KeyboardInterrupt:
print 'closing json file'
dictionary_file.seek(-1, os.SEEK_END)
dictionary_file.truncate()
dictionary_file.write(']')
dictionary_file.close()
return False
print 'closing json file'
dictionary_file.seek(-1, os.SEEK_END)
dictionary_file.truncate()
dictionary_file.write(']')
dictionary_file.close()
return True
def scrape_observations(year):
#get all the nodes that point to observations
page = requests.get('http://mushroomobserver.org/sitemap/index.html')
tree = html.fromstring(page.content)
observations_list = tree.xpath('//a[contains(@href, "observations")]/@href')
observations_list = observations_list[::-1]
for index in range(len(observations_list)):
observations = observations_list[index]
page = requests.get('http://mushroomobserver.org/sitemap/'+observations)
tree = html.fromstring(page.content)
obs = tree.xpath('//a[contains(@href, "show_observation")]/@href')
return_value = download_images(obs,year)
if return_value==False:
break
year = int(sys.argv[1])
scrape_observations(year)