-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixabay_final.py
85 lines (71 loc) · 2.89 KB
/
pixabay_final.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
import requests
import re
import hashlib
from pprint import pprint
import os
import time
import sys
from random import random
from multiprocessing import Pool
from functools import partial
# ignore InsecureRequestWarning
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def img_name_processor(src):
"""
This function is used to handle the file name of the saved picture.
Hash the URL of the picture as its filename.
:param src: image url
:return: image filename
"""
h5 = hashlib.md5()
h5.update(src.encode('utf-8'))
img = h5.hexdigest() + '.jpg'
return img
def download_images(link, path):
image_name = img_name_processor(link)
print('Download image {}...'.format(image_name))
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/68.0.3440.106 Safari/537.36'}
pic = requests.get(link, timeout=3, headers=headers, verify=False)
except Exception as e:
print(f'{e}')
return False
else:
if pic.status_code == 200:
img = pic.content
file = os.path.join(path, image_name)
if os.path.exists(file):
print(f'{file} exists.')
return
with open(file, 'wb') as f:
f.write(img)
time.sleep(random())
return True
def get_pic_num_and_page_num(keyword):
url = f'https://pixabay.com/zh/images/search/{keyword}/'
res = requests.get(url)
assert "抱歉,我们没找到相关信息。" not in res.text, "抱歉,我们没找到相关信息。"
pic_num = re.findall('<h1 .*>(.*)</h1>', res.text)[0].split()[0]
print(f'There are total {pic_num} images about {keyword}')
comment = re.compile('<input name=\"pagi\"(.*?) ', re.DOTALL)
page_num = comment.findall(res.text)[0].split()[-1]
print(f'There are total {page_num} pages of images')
return pic_num, page_num
def pixabay_spider(keyword, path):
assert keyword is not None, "please input keyword"
path = os.path.join(path, keyword)
if not os.path.exists(path):
os.makedirs(path)
pic_num, page_num = get_pic_num_and_page_num(keyword)
for page in range(1, int(page_num) + 1):
url = f'https://pixabay.com/zh/images/search/{keyword}/?pagi={page}'
res = requests.get(url)
links = re.findall('data-lazy=\"(.*?)\"', res.text) # part one image links, total 84
links += re.findall('img srcset=\".*?\" src=\"(.*?)\"', res.text) # part two image links, total 16, all is 100
pool = Pool(processes=4)
pool.map(partial(download_images, path=path), links)
if __name__ == '__main__':
pixabay_spider(keyword='cat', path='D:/spider/pixabay')