-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape_species.py
214 lines (155 loc) · 6.48 KB
/
scrape_species.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
'''Scrape species pages'''
import argparse, csv, os, re, time
from datetime import date
import utils.scrape as us
import utils.setup as setup
def get_taxon_page_fields(url:str, output_list:list, base_url:str, run_count:int) -> list:
'''get fields on a species detail page'''
if re.match(base_url, url) is not None:
url = re.sub(base_url, '', url)
print(f'{run_count} = {base_url}{url}')
soup = us.get_page_soup(base_url + url)
page = {
'order': run_count,
'url': base_url + url,
'page_type': 'Species Detail-page',
'images':None,
'descrip_text_raw':None,
'type_descrip':None,
'type_locality':None,
'measurements_raw':None,
'mea_head_body':None,
'mea_tail':None,
'mea_weight':None,
'key_reference': None,
'taxon': None,
'author_year': None,
'common': None,
'image_credits':None
}
# Add match-taxon
page['taxon'] = us.get_text_from_soup(soup, 'h1')
page['common'] = us.get_text_from_soup(soup, 'h3')
p_ids = us.get_html_from_soup(soup, 'p')
if len(p_ids) > 0:
for p in p_ids:
if p.attrs is not None:
if 'id' in p.attrs:
if p['id'] == "zero_margin":
page['author_year'] = p.text.strip()
# Add images
page_images = us.get_html_from_soup(soup=soup, selector="img")
page_image_list = []
if len(page_images) > 0:
for image in page_images:
if 'alt' not in image.attrs:
page_image_list.append(base_url + image['src'])
if len(page_image_list) > 0:
page['images'] = ' | '.join(page_image_list).strip()
# Get Match-page Text:
page_text_chunks = us.get_html_from_soup(soup=soup, selector="div")
page_text_list = []
if len(page_text_chunks) > 0:
for chunk in page_text_chunks:
if 'id' in chunk.attrs:
if chunk['id'] == 'content_main':
page_text_list.append(chunk.text)
if len(page_text_list) > 0:
page['descrip_text_raw'] = ' | '.join(page_text_list)
if 'Select species from list' in page['descrip_text_raw']: return
if page['descrip_text_raw'] is not None:
# Get Type Descrip
page['type_descrip'] = re.sub(
r'(.*\n)*(Type Description\:\n*)(.+)(\nType Locality.*)(\n*.)*', r'\3',
page['descrip_text_raw']).strip()
# Get Type Locality
page['type_locality'] = re.sub(
r'(.*\n)*(Type Locality\:\n)(.*)(\nMeasurements.*)(\n*.)*', r'\3',
page['descrip_text_raw']).strip()
if page['type_locality'] == page['descrip_text_raw']:
page['type_locality'] = None
# Get Measurements
page['measurements_raw'] = re.sub(
r'(.*\n)*(Measurements\:\n*)(.+)(g[A-Z].+)(\n*.)*', r'\3g',
page['descrip_text_raw']).strip()
if page['measurements_raw'] is not None:
# Get Head/Body
page['mea_head_body'] = re.sub(r'(Head and body\:\s)(.+)(mTail.*)', r'\2m', page['measurements_raw'])
# Get Tail
page['mea_tail'] = re.sub(r'(.*Tail length\:\s)(.+)(mWeight.*)', r'\2m', page['measurements_raw'])
# Get Weight
page['mea_weight'] = re.sub(r'(.*Weight\:\s)(.+)', r'\2', page['measurements_raw']).strip()
# Get Key reference
page['key_reference'] = re.sub(
r'(.|\n)*(Key Reference\:\n*)(.+)', r'\3',
page['descrip_text_raw']).strip()
# get image credit
# <p id="credit">
#
# Get list of Match-page image-URLs
page_images = us.get_html_from_soup(soup=soup, selector="img")
page_image_list = []
if len(page_images) > 0:
for image in page_images:
if 'alt' not in image.attrs:
page_image_list.append(base_url + image['src'])
if len(page_image_list) > 0:
page['images'] = ' | '.join(page_image_list)
# Get Image credits
page_image_credits = us.get_html_from_soup(soup=soup, selector="font")
page_credit_list = []
for credit in page_image_credits:
if 'size' in credit.attrs:
if credit['size'] == '2':
page_credit_list.append(credit.text)
if len(page_credit_list) > 0:
page['image_credits'] = ' | '.join(page_credit_list)
if page not in output_list:
output_list.append(page)
time.sleep(4)
return
def main():
'''main function'''
config = setup.get_config()
base_url = config['BASE_URL']
# Set up command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("start_url", help="top level URL to use as starting point")
parser.add_argument("output_path", help="Output path (with trailing '/')")
args = parser.parse_args()
run_count = 0
# First, we need to get all of the URLs we need to scrape
start_url = args.start_url
soup = us.get_page_soup(start_url)
soup_links = us.get_html_from_soup(soup=soup, selector="a")
soup_species_list = []
# Get Taxon URLs
if soup_links is not None and len(soup_links) > 0:
for species_link in soup_links:
if "species" in species_link['href']:
species_link_url = species_link['href']
if re.match(base_url, species_link_url) is not None:
species_link_url = re.sub(base_url, '', species_link_url)
species_url = {'url':base_url + species_link_url}
if species_url not in soup_species_list:
soup_species_list.append(species_url)
output_list = []
# # Test small batch
# soup_species_list = soup_species_list[0:5]
if len(soup_species_list) > 0:
for species in soup_species_list:
run_count += 1
get_taxon_page_fields(species['url'], output_list, base_url, run_count)
# Check if dir exists, and if not, make it
output_path = args.output_path
if not os.path.isdir(output_path):
os.makedirs(output_path)
date_suffix = re.sub(r'\-|\s*|\:|\..*', '', str(date.today()))
with open(f"{output_path}scraped_taxon_{date_suffix}.csv", encoding='utf-8', mode='w') as scraped_urls:
col_names = list(output_list[0].keys())
print(col_names)
write = csv.DictWriter(f=scraped_urls, fieldnames=col_names)
write.writeheader()
write.writerows(output_list)
if __name__ == '__main__':
main()