XKCD刮板自动钻孔机

2024-05-02 23:23:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前在ATBS的第11章,并通过网络刮板项目的工作。我可以让它运行良好,但网络漫画从来没有真正下载到我的Mac上。你知道吗

#! /usr/bin/env python3

#downloadXkcd.py - Downloads every single XKCD comic.

import requests, os, bs4

url = 'http://xkcd.com'             # starting URL
os.makedirs('xkcd', exist_ok=True)  # store comics in ./xkcd

while not url.endswith('#'):

    #TODO: DL the page
    print('Downloading page %s...' % url)
    res = requests.get(url)
    res.raise_for_status()

    soup = bs4.BeautifulSoup(res.text)

    #TODO: Find URL of image
    comicElem = soup.select('#comic img')
    if comicElem == []:
        print('Could not find comic image.')
    else:
        comicUrl = 'http:' + comicElem[0].get('src')

        #TODO: Download Image
        print('Downloading image %s' % (comicUrl))
        res = requests.get(comicUrl)
        res.raise_for_status()

        #TODO: Save image to ./xkcd
        imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb')
        for chunk in res.iter_content(100000):
            imageFile.write(chunk)
        imageFile.close()

    #TODO: Get prev button URL
    prevLink = soup.select('a[rel="prev"]')[0]
    url = 'http://xkcd.com' + prevLink.get('href')

print('Done.')

我需要修复什么才能下载漫画?谢谢。你知道吗


Tags: imagehttpurlforgetosresrequests