【Class 48】【实例】python爬虫实现 下载所有 XKCD 漫画

下载所有 XKCD 漫画

#! python3
# -*- coding: utf-8 -*-

import requests, os, bs4

url = "http://xkcd.com"
os.makedirs('xkcd',exist_ok=True)

while not url.endswith('#'):

    # 下载漫画
    print('Downloading page %s...' % url)
    res = requests.get(url)
    res.raise_for_status()

    # 解析网页
    soup = bs4.BeautifulSoup(res.text,'html.parser')

    # 寻找图片链接
    comicElem = soup.select('#comic img')
    if comicElem == []:
        print('Could not find comic image.')
    else:
        comicUrl = 'http:'+comicElem[0].get('src')

    # 下载图片
    print('Downloading image %s...' % (comicUrl))
    res = requests.get(comicUrl)
    res.raise_for_status()

    # 保存图片
    imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb')
    for chunk in res.iter_content(100000):
        imageFile.write(chunk)
    imageFile.close()

    # 获得前一页的链接
    prevLink = soup.select('a[rel="prev"]')[0]
    url = 'http://xkcd.com' + prevLink.get('href')

print('Done')

下载结果:

Downloading image http://imgs.xkcd.com/comics/light_pollution.png...
Downloading page http://xkcd.com/2120/...
Downloading image http://imgs.xkcd.com/comics/brain_hemispheres.png...
Downloading page http://xkcd.com/2119/...
Downloading image http://imgs.xkcd.com/comics/video_orientation.png...
Downloading page http://xkcd.com/2118/...
Downloading image http://imgs.xkcd.com/comics/normal_distribution.png...
Downloading page http://xkcd.com/2117/...
Downloading image http://imgs.xkcd.com/comics/differentiation_and_integration.png...
Downloading page http://xkcd.com/2116/...
Downloading image http://imgs.xkcd.com/comics/norm_normal_file_format.png...
Downloading page http://xkcd.com/2115/...
Downloading image http://imgs.xkcd.com/comics/plutonium.png...
Downloading page http://xkcd.com/2114/...
Downloading image http://imgs.xkcd.com/comics/launch_conditions.png...
Downloading page http://xkcd.com/2113/...
Downloading image http://imgs.xkcd.com/comics/physics_suppression.png...
Downloading page http://xkcd.com/2112/...
Downloading image http://imgs.xkcd.com/comics/night_shift.png...
Downloading page http://xkcd.com/2111/...
Downloading image http://imgs.xkcd.com/comics/opportunity_rover.png...
Downloading page http://xkcd.com/2110/...
Downloading image http://imgs.xkcd.com/comics/error_bars.png...

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

"小夜猫&小懒虫&小财迷"的男人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值