-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_object_details.py
53 lines (46 loc) · 1.64 KB
/
scrape_object_details.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
import requests
from bs4 import BeautifulSoup
import csv
import sys
with open(sys.argv[1]) as f:
lines = f.read().splitlines()
items = []
for line in lines:
object = { # Collected details
"objectId": line.split('/')[-1],
'objectUrl': line,
'images': [],
'title': '',
'object_name': '',
'artist': '',
'date': '',
'description': '',
'dimensions': '',
'credit_line': '',
'collection': '',
'object_number': ''
}
if 'webobject' in line:
r = requests.get(line)
soup = BeautifulSoup(r.text, 'html.parser')
related_photos = soup.findAll("div", {"class": "relatedPhotos"})
for photo in related_photos:
for a in photo.find_all('a', href=True):
object["images"].append(a['href'])
table_details = soup.find('table', {'class': 'interiorResultTable'})
table_rows = table_details.find_all('tr')
for table_row in table_rows:
category = table_row.find('td', {'class': 'category'})
display = table_row.find('td', {'class': 'display'})
key = '_'.join(category.text.lower().split(' '))
object[key] = display.text.strip()
items.append(object)
with open(sys.argv[2], 'w') as f:
writer = csv.writer(f)
for item in items:
writer.writerow([
item['objectId'], item['objectUrl'], item['images'],
item['title'], item['object_name'], item['artist'],
item['date'], item['description'], item['dimensions'],
item['credit_line'], item['collection'], item['object_number']
])