#code=utf8
#爬豆瓣首页图片

import urllib.request,socket,re,sys,os

#定义文件保存路径
targetPath=r"E:\user\Github\test\pic"

def saveFile(path):
    #检测路径有效性
    if not os.path.isdir(targetPath):
        os.mkdir(targetPath)

    #设置图片路径
    pos=path.rindex('/')
    t=os.path.join(targetPath,path[pos+1:])
    return t

#用if __name__=='__main__'来判断是否在运行

url="https://www.douban.com/"
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36'}

req=urllib.request.Request(url=url,headers=headers)

res=urllib.request.urlopen(req)

data=res.read()

for link,t in set(re.findall(r'(https:[^\s]*?(jpg|png|gif))',str(data))):
    print(link,t)
    try:
        urllib.request.urlretrieve(link,saveFile(link))
    except:
        print('失败')