28-第2步_下载网页
第2步:下载网页
我们来实现下载网页的代码。让你的代码看起来像这样:
#! python3
# downloadXkcd.py - Downloads every single XKCD comic.
import requests, os, bs4
url = 'https://' # starting url
os.makedirs('xkcd',exist_ok=True) # store comics in ./xkcd
while not url.endswith('#'):
# Download the page.
print('Downloading page %s...' % url)
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
# TODO: Find the URL of the comic image.
# TODO: Download the image.
# TODO: Save the image to ./xkcd.
# TODO: Get the Prev button's url.
print('Done.')
首先,输出 url
,这样用户就知道程序将要下载哪个URL。然后利用 requests
模块的 request.get()
函数下载它。像以往一样,马上调用 Response
对象的 raise_for_status()
方法,如果下载发生问题,就抛出异常,并终止程序;否则,利用下载页面的文本创建一个 BeautifulSoup
对象。