-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmzitu.py
53 lines (49 loc) · 1.86 KB
/
mzitu.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
#!/usr/bin/env python3
# encoding=utf-8
# 作者:孙士标
# 博客地址:http://bbiao.me
# 日期:2017-9-24
from bs4 import BeautifulSoup
import requests
import os
import time
from multiprocessing.pool import Pool
headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
'Referer': 'http://www.mzitu.com/'
}
def get_pic(pag):
url = 'http://www.mzitu.com/page/' + str(pag)
html = requests.get(url, headers=headers).content
soup = BeautifulSoup(html, 'lxml')
links = soup.find('ul', attrs={'id': 'pins'}).find_all('li')
for link in links:
n = 1
title = link.find('span').getText()
detail_link = link.find('a')['href']
xml = requests.get(detail_link).content
soups = BeautifulSoup(xml, 'lxml')
pages = soups.find('div', attrs={'class': 'pagenavi'}).find_all('span')[-2].getText()
dirname = u'[{}P] {}'.format(int(pages), title)
if not os.path.exists(dirname):
os.mkdir(dirname)
for page in range(1, int(pages)+1):
each_pic = detail_link + '/' + str(page)
picture = requests.get(each_pic, headers=headers).content
pic_html = BeautifulSoup(picture, 'lxml')
img = pic_html.find('div', attrs={'class': 'main-image'}).find('img')['src']
filename = '%s/%s/%s.jpg' % (os.path.abspath('.'), dirname, n)
print(u'开始下载图片:%s 第%s张' % (dirname, n))
try:
with open(filename, 'wb+') as jpg:
jpg.write(requests.get(img, headers=headers).content)
n += 1
time.sleep(1)
except:
pass
if __name__ == '__main__':
pool = Pool(10)
page = [x for x in range(1, 154)]
pool.map(get_pic, page)
pool.close()
pool.join()