【下载所有XKCD漫画】 详细解析

目录

 1,实现目的

2,准备工作

2.1,确保所需模块已安装

2.2,脚本思路

3,全部代码

4,执行结果

1,实现目的

XKCD 是一个流行的极客漫画网站。首页 http://xkcd.com/有一个“Prev”按钮,让用户导航到前面的漫画。

Python脚本实现:从第1幅到第n幅(用户输入),下载这些XKCD网上的漫画。

2,准备工作

2.1,确保所需模块已安装

本脚本需要 requests, bs4, os 三个模块,os是Python自带,其余需要自己安装。不会请参考通过命令行安装 Python的第三方模块

2.2,脚本思路

•  提示用户键入一个数字,获得起始的URL。

•  利用 requests 模块下载页面。

•  利用 Beautiful Soup 找到页面中漫画图像的 URL。(要清楚该网站的HTML结构)

•  利用 iter_content()下载漫画图像,并保存到硬盘。

•  找到前一张漫画的链接 URL,然后重复。

XKCD网站第一幅漫画URL为http://xkcd.com/1/ ,再点击“Prev”按钮会发现URL为http://xkcd.com/1/# ,那么就可以通过这个来作为循环的终止条件

3,全部代码

#! python3
#This script is used to download single XKCD comic.
import requests, os, bs4
#download comic from 1 to n
print('Enter num')
url = 'http://xkcd.com/' + input() + '/'

os.makedirs('xkcd', exist_ok = True)
while not url.endswith('#'):   #the first comic's url ends with '#'. https://xkcd.com/1/#
	#Download the page
	print('Downloading page  | %s...' %url)
	res = requests.get(url)
	res.raise_for_status()
	
	soup = bs4.BeautifulSoup(res.text, features = 'html.parser')
	
	#Find URL of the comic image
	comicElem = soup.select('#comic img')
	if comicElem == []:
		print("Could not find comic image")
	else:
		comicUrl = 'http:' + comicElem[0].get('src')
	
	#Download the image
		print('Downloading image | %s...' %comicUrl)
		res = requests.get(comicUrl)
		res.raise_for_status()
		
	#Save the 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()
		
	#Get the Prev button's url
	prevLink = soup.select('a[rel="prev"]')[0]
	url = 'http://xkcd.com' + prevLink.get('href')
	
print('DONE')

4,执行结果

在IDLE上执行,键入5,可以看到硬盘中下载了1到5,共5幅图片

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值