代码如下:
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
import requests
def save_pic(pic, save_path):
pic_html = requests.get(pic)
with open(save_path, "wb") as pic_file:
pic_file.write(pic_html.content)
url = 'https://www.xkcd.com/'
for i in range(1982):
number = i+1
new_url = url + str(number)
print(new_url)
get_url = requests.get(new_url)
try:
get_url.raise_for_status()
except:
print('---error---')
bsObj = BeautifulSoup(get_url.text,'html.parser')
comic = bsObj.find(id = "comic")
try:
pic = comic.img['src']
pic = 'https:' + pic
save_path = '/Users/apple/Desktop/北大青鸟作业/第三周作业/作业完成/jpg/Xkcd/' + str(number)