import requests,bs4,os


url =  'https://xkcd.com/'




#os.makedirs('xkcd',exist_ok=True)#本地目录前创建保存漫画的文件夹
i=0#下载漫画计数


while not url.endswith('#'):
    #下载漫画网页,并匹配查找漫画下载路径
    res = requests.get(url)    
    try:
        resImage.raise_for_status()
    except Exception as exc:
        print('There was a problem:%s'%(exc))
        
    Soup = bs4.BeautifulSoup(res.text)
    comicElems = Soup.select('div#comic img')
    print(comicElems[0])


    if comicElems == []:
        print('Could not find comic image!')
    else:
        comicUrl = 'http:'+str(comicElems[0].get('src'))#拼接漫画下载路径
        print('Downing image %s....'%(comicUrl))
        resImage = requests.get(comicUrl)
        try:
            resImage.raise_for_status()
        except Exception as exc:
            print('There was a problem:%s'%(exc))
        
        #下载漫画存放在文件picturer中


        openFile = open('C:\\Users\\Nick\\Desktop\\python\\drawing\\2\\picture\\'+str(i)+'.png','ab')#下载并重新命名
        i = i+1
        for chunk in resImage.iter_content(10000):
            openFile.write(chunk)
        openFile.close()
        
    #拼接上一页网页路径    
    prevLink = Soup.select('a[rel="prev"]')[0]
    url = 'https://xkcd.com/'+str(prevLink.get('href'))


print("Done!")

本文转载:CSDN博客