-
Notifications
You must be signed in to change notification settings - Fork 0
/
freepngs.py
68 lines (56 loc) · 2.15 KB
/
freepngs.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
""" Created by MrBBS - 3/28/2023 """
# -*-encoding:utf-8-*-
import requests
from bs4 import BeautifulSoup
from tqdm import tqdm
from pathlib import Path
import random
import string
import multiprocessing
def download(url, out: Path):
name = ''.join(random.choices(string.ascii_letters + string.digits, k=10)) + '.png'
r = requests.get(url)
if r.status_code == 200:
with open(out.joinpath(name).as_posix(), 'wb') as f:
f.write(r.content)
def download_image(url, out):
res = requests.get(url).text
soup = BeautifulSoup(res, 'html.parser')
container = soup.find_all('div', attrs={
'class': 'gallery-item-container item-container-regular has-custom-focus visible clickable'})
pool = multiprocessing.Pool(processes=12)
jobs = []
for div in container:
try:
link = div.find('source')
img = link['srcset'].split('png')[0]
jobs.append(pool.apply_async(download, args=(img + 'png', out), callback=None))
except:
pass
pool.close()
for job in tqdm(jobs, position=1, leave=False):
job.get()
def get_image(url, out):
out.mkdir(parents=True, exist_ok=True)
res = requests.get(url).text
soup = BeautifulSoup(res, 'html.parser')
container = soup.find('div', attrs={'aria-label': 'Matrix gallery'})
if container is not None:
for collection in container.find_all('a'):
try:
link = collection['href']
c = link.split('/')[-1].split('-')[0]
o = out.joinpath(c)
o.mkdir(parents=True, exist_ok=True)
download_image(collection['href'], o)
except:
pass
# break
if __name__ == '__main__':
url = 'https://www.freepngs.com/'
output = Path(r'E:\segment_image\crawler')
res = requests.get(url).text
soup = BeautifulSoup(res, 'html.parser')
container = soup.find('div', attrs={'aria-label': 'Matrix gallery'})
for collection in tqdm(container.find_all('a')[23:], position=0):
get_image(collection['href'], output)