#!/usr/bin/env python3
#中国国家地理图片 url = http://www.dili360.com/gallery/cate/1.htm

def download(i):
    name = i[0]
    url = i[1]
    rs = r.get(url, stream = True)
    name = name+'.png'
    with open('./img/{}'.format(name), 'wb') as f:
        for chunk in rs.iter_content(chunk_size = 128):
            f.write(chunk)
        print('loaded {}'.format(name))

def downloadAllPictures(aurl):
    res = r.get(aurl)
    res.encodng = 'utf-8'
    soup = bs(res.text, 'html.parser')
    lst = soup.select('.gallery-block-small')
    l = lst[0].find_all("img")
    newl = []
    for i in range(len(l)):
        if i%2 == 0:
            newl.append(l[i])
    gallary = []
    for i in newl:
        title = i['alt']
        link = i['src']
        gallary.append([title, link])
    for i in gallary:
        download(i)

if __name__ == '__main__':

    import requests as r
    from bs4 import BeautifulSoup as bs
    import os
    os.makedirs('./img/', exist_ok=True)  #在Jupter上建立一个文件夹来保存图片

    aurl = 'http://www.dili360.com/gallery/cate/1.htm'
    downloadAllPictures(aurl)
