-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscrape_nendroids2.py
68 lines (52 loc) · 1.97 KB
/
scrape_nendroids2.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
#!/usr/bin/env python
# Scrapes the international catalogue that has discontinued products included.
import os
import os.path
from bs4 import BeautifulSoup
import requests
from pathvalidate import sanitize_filepath
from multiprocessing.pool import ThreadPool as Pool
def worker(link):
resp = requests.get(link)
page = BeautifulSoup(resp.text, features="html5lib")
produkt_gallery = page.find(class_="itemPhotos")
if not produkt_gallery:
print(f"*** SKIPPING LINK (no images): {link}")
return
for img in produkt_gallery.find_all("img", itemprop="image"):
url = "https:" + img.get("src")
heading = page.find(itemprop="name").text
path = sanitize_filepath("D:/media/figure/" + heading.replace("/", "-") + "/" + os.path.basename(url), platform="Windows")
print(url)
print(path)
os.makedirs(os.path.dirname(path), exist_ok=True)
resp = requests.get(url, stream=True)
if os.path.exists(path):
print("*** SKIPPING (exists): " + path)
else:
print(f"Saving: {heading} - {path}")
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)
txt = f"{heading}"
with open(txt_path, "w", encoding="utf-8") as f:
f.write(txt)
BASE_URL = "https://www.goodsmile.info/en/products/category/scale/"
no = 1
while True:
print(f"=== Page {no} ===")
resp = requests.get(f"{BASE_URL}/page/{no}")
page = BeautifulSoup(resp.text, features="html5lib")
produkts = page.find(class_="hitList")
links = list(set(filter(lambda x: x, [a.get("href") for a in produkts.find_all("a")])))
if not links:
print("Finished.")
break
p = Pool(processes=8)
for res in p.imap_unordered(worker, links):
pass
p.close()
p.join()
no += 1