-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscrape_artnet.py
90 lines (75 loc) · 2.44 KB
/
scrape_artnet.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
#!/usr/bin/env python
import os
import os.path
import sys
import urllib.parse
import re
import json
from bs4 import BeautifulSoup
import requests
from pathvalidate import sanitize_filepath
from multiprocessing.pool import ThreadPool as Pool
from urllib.parse import urlparse, parse_qs, urljoin
artist = sys.argv[1]
work_type = sys.argv[2]
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
"Content-Type": "application/json; charset=UTF-8",
}
def worker(item):
resp = requests.get(urljoin("https://www.artnet.com", item["ImgHref"]), headers=HEADERS)
page = BeautifulSoup(resp.text, features="html5lib")
imgArea = page.find(id="imgArea")
if not imgArea:
print(f"*** SKIPPING (no images): {item['ImgHref']}")
return
img = imgArea.find("img")
if not img:
print(f"*** SKIPPING (no images): {item['ImgHref']}")
return
url = "https:" + img.get("src")
path = sanitize_filepath(os.path.join("artnet", artist, os.path.basename(url)))
os.makedirs(os.path.dirname(path), exist_ok=True)
if os.path.exists(path):
print("*** SKIPPING (exists): " + path)
else:
resp = requests.get(url, headers=HEADERS)
if resp.status_code != 200:
print(f"!!! FAILED saving: {url}")
return
print("Saving: " + url)
with open(path, "wb") as f:
for chunk in resp:
f.write(chunk)
artist_name = item["ArtistName"]
artwork_name = item["ArtworkTitle"]
year = item["WorkYearFrom"]
txt = f"{artist_name}, {artwork_name}, {year}"
txt_name = os.path.splitext(os.path.basename(path))[0] + ".txt"
txt_path = os.path.join(os.path.dirname(path), txt_name)
with open(txt_path, "w", encoding="utf-8") as f:
f.write(txt)
no = 1
while True:
print(f"=== Page {no} ===")
params = {
"artistId": artist,
"type": work_type,
"page": no
}
resp = requests.get("https://www.artnet.com/artists/ArtistOverview.aspx/GetArtworks", params=params, headers=HEADERS)
resp.raise_for_status()
resp = resp.json()
if "d" not in resp:
print("Finished.")
break
j = json.loads(resp["d"])
if not j["items"]:
print("Finished.")
break
p = Pool(processes=8)
for res in p.imap_unordered(worker, j["items"]):
pass
p.close()
p.join()
no += 1