20-第1步_修改程序以使用函数
第1步:修改程序以使用函数
该程序的大部分是来自第12章的代码,所以我会跳过 Requests
和 BeautifulSoup
代码的解释。需要完成的主要变更是导入 threading
模块,并定义 downloadXkcd()
函数,该函数接收开始和结束的漫画编号作为参数。
例如,调用 downloadXkcd(140,280)
将循环执行下载代码,依次下载漫画。你创建的每个线程都会调用 downloadXkcd()
,并传入不同范围的漫画进行下载。
将下面的代码添加到threadedDownloadXkcd.py程序中:
#! python3
# threadedDownloadXkcd.py - Downloads XKCD comics using multiple threads.
import requests, os, bs4, threading
❶ os.makedirs('xkcd', exist_ok=True) # store comics in ./xkcd
❷ def downloadXkcd(startComic, endComic):
❸ for urlNumber in range(startComic, endComic):
# Download the page.
print('Downloading page http:// __/%s...' % (urlNumber))
❹ res = requests.get('http:// __/%s' % (urlNumber))
res.raise_for_status()
❺ soup = bs4.BeautifulSoup(res.text, 'html.parser')
# Find the URL of the comic image.
❻ comicElem = soup.select('#comic img')
if comicElem == []:
print('Could not find comic image.')
else:
❼ comicUrl = comicElem[0].get('src')
# Download the image.
print('Downloading image %s...' % (comicUrl))
❽ res = requests.get('https:'+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()
# TODO: Create and start the Thread objects.
# TODO: Wait for all threads to end.
导入需要的模块后,❶行创建了一个目录来保存漫画,然后开始定义 downloadXkcd()
❷。循环遍历指定范围中的所有编号❸,并下载每个页面❹。用 Beautiful Soup
查看每一页的HTML❺以找到漫画图像❻。如果页面上没有当前漫画图像,就输出一条消息;否则,取得图片的URL❼,并下载图像❽。最后,将图像保存到我们创建的目录中。