2016-07-14 155 views
-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.') 
+0

請提供包含'a'的HTML相關代碼片段,以便您想要下載的圖片。 – 2016-07-14 13:32:32

回答

0

你的問題是,你保存圖像在你的異常塊,取消縮進。下載文件對象的更簡單的方法是使用shutil。

import requests, os, bs4, shutil 

url = 'http://xkcd.com' # starting rule 
if not os.path.exists('xkcd'): 
    os.makedirs('xkcd') # 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: 
    comicURL = "http:"+comicElem[0].get('src') 
    response = requests.get(comicURL, stream=True) 
    with open('xkcd/img.png', 'wb') as out_file: 
     shutil.copyfileobj(response.raw, out_file) 
    del response 



print('Done.')