# _*_ coding: utf-8 _*_
import urllib.request
import re
import os
def ck_dir(folder='pic'):
if not os.path.exists(folder):
os.mkdir(folder)
os.chdir(folder)
def main():
ck_dir()
url = 'http://jandan.net/pic'
for i in range(3):
req = urllib.request.Request(url)
req.add_header('user-agent', 'mozilla')
r = urllib.request.urlopen(req)
html = r.read().decode('utf-8')
result = re.findall(r'<img src="(.*?)"', html)
nx_page = re.findall(r'href="(.*?)" class="previous', html)[0]
url = 'http:' + nx_page
dl_img(result)
def dl_img(ls):
for item in ls:
img_url = 'http:' + item
img_name = item.split('/')[-1]
urllib.request.urlretrieve(img_url, img_name)
if __name__ == '__main__':
main()
|