-4
我已經寫了一個腳本從xkcd漫畫網站下載單個圖像。但是腳本通常運行並且不下載任何圖像。有什麼問題 ?任何幫助,將不勝感激。這裏的代碼:無法通過python腳本下載圖像
#! python3
import requests, os, bs4
url = 'http://xkcd.com' # starting rule
os.makedirs('xkcd', exist_ok=True) # store comics in ./xkcd
# Download the page
print('Downloading the page %s...' % url)
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text)
# Find the URL of the comic image
comicElem = soup.select('#comic img')
if comicElem == []:
print('Could not find comic image.')
else:
try:
comicURL = 'http:' + comicElem[0].get('src')
# Download the image
print('Downloading image %s...' % (comicURL))
res = requests.get(comicURL)
res.raise_for_status()
except requests.exceptions.MissingSchema:
# skip this comic
prevLink = soup.select('a[rel="prev"]')[0]
url = 'http://xkcd.com' + prevLink.get('href')
# continue
# 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()
print('Done.')
請提供包含'a'的HTML相關代碼片段,以便您想要下載的圖片。 – 2016-07-14 13:32:32