-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrape_wikiart.py
52 lines (40 loc) · 1.44 KB
/
scrape_wikiart.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
#!/usr/bin/env python
import os
import os.path
import sys
from bs4 import BeautifulSoup
import requests
from pathvalidate import sanitize_filepath
from multiprocessing.pool import ThreadPool as Pool
ARTIST = sys.argv[1]
def worker(painting):
path = sanitize_filepath(os.path.join("wikiart", ARTIST, os.path.basename(painting["image"])))
os.makedirs(os.path.dirname(path), exist_ok=True)
if os.path.exists(path):
print("*** SKIPPING (exists): " + path)
else:
resp = requests.get(painting["image"])
print("Saving: " + painting["image"])
with open(path, "wb") as f:
for chunk in resp:
f.write(chunk)
txt_name = os.path.splitext(os.path.basename(path))[0] + ".txt"
txt_path = os.path.join(os.path.dirname(path), txt_name)
title = painting['title'].replace(",", "")
txt = f"{painting['artistName']}, {title}, {painting['year']}"
with open(txt_path, "w", encoding="utf-8") as f:
f.write(txt)
no = 1
while True:
print(f"=== Page {no} ===")
resp = requests.get(f"https://www.wikiart.org/en/{ARTIST}/mode/all-paintings", params={"json": 2, "layout": "new", "page": no, "resultType": "masonry"})
resp = resp.json()
if "Paintings" not in resp or resp["Paintings"] is None:
print("Finished.")
exit(0)
p = Pool(processes=8)
for res in p.imap_unordered(worker, resp["Paintings"]):
pass
p.close()
p.join()
no += 1