当前位置:嗨网首页>书籍在线阅读

29-第3步_寻找和下载漫画图像

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

第3步:寻找和下载漫画图像

让你的代码看起来像这样:

#! python3
# downloadXkcd.py - Downloads every single XKCD comic.
import requests, os, bs4
--snip--
    # Find the URL of the comic image.
    comicElem = soup.select('#comic img')
    if comicElem == []:
        print('Could not find comic image.') 
    else:
        comicUrl = 'https:' + comicElem[0].get('src')
        # Download the image.
        print('Downloading image %s...' % (comicUrl))
        res = requests.get(comicUrl)
        res.raise_for_status()
    # TODO: Save the image to ./xkcd.
    # TODO: Get the Prev button's url.
print('Done.')

用开发者工具检查XKCD主页后,你知道漫画图像的 <img> 元素在 <div> 元素中, <div> 带有的 id 属性设置为 comic 。选择器 '#comic img' 将从 BeautifulSoup 对象中选出正确的 <img> 元素。

有一些XKCD页面有特殊的内容,不是一个简单的图像文件。这没问题,跳过它们就好了。如果选择器没有找到任何元素,那么 soup.select('#comic img') 将返回一个空的列表。出现这种情况时,程序将输出一条错误信息,不下载图像,并继续执行。

否则,选择器将返回一个包含一个 <img> 元素的列表。可以从这个 <img> 元素中取得 src 属性,将 src 传递给 requests.get() ,以下载这个漫画的图像文件。