python实现从网站XKCD下载全部漫画

来源:互联网 发布:windows select epoll 编辑:程序博客网 时间:2024/06/05 16:51
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!")
0 0
原创粉丝点击