Я в настоящее время нахожусь на Главе 11 ATBS и работаю через проект Web Scraper. Я могу заставить его работать нормально, но на моем Mac никогда не загружаются веб-комиксы.
#! /usr/bin/env python3
#downloadXkcd.py - Downloads every single XKCD comic.
import requests, os, bs4
url = 'http://xkcd.com' # starting URL
os.makedirs('xkcd', exist_ok=True) # store comics in ./xkcd
while not url.endswith('#'):
#TODO: DL the page
print('Downloading page %s...' % url)
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text)
#TODO: Find URL of image
comicElem = soup.select('#comic img')
if comicElem == []:
print('Could not find comic image.')
else:
comicUrl = 'http:' + comicElem[0].get('src')
#TODO: Download Image
print('Downloading image %s' % (comicUrl))
res = requests.get(comicUrl)
res.raise_for_status()
#TODO: Save 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: Get prev button URL
prevLink = soup.select('a[rel="prev"]')[0]
url = 'http://xkcd.com' + prevLink.get('href')
print('Done.')
Что нужно исправить, чтобы скачать комиксы? Спасибо.