2016-09-10 60 views
0

我想從NGA.gov網站使用python 3和urllib下載圖片。如何從網站下載沒有明顯延伸的圖片?

該網站不顯示在一個標準的時尚JPG格式圖像和我得到一個錯誤。

import urllib.request 
from bs4 import BeautifulSoup 


try: 
    with urllib.request.urlopen("http://images.nga.gov/?service=asset&action=show_preview&asset=33643") as url: 
     s = url.read() 

    soup = BeautifulSoup(s, 'html.parser') 


    img = soup.find("img") 
    urllib.request.urlretrieve(img,"C:\art.jpg") 

except Exception as e: 
    print (e) 

錯誤: 某些字符不能被解碼,並用替換字符取代。 預期字符串或字節狀物體

可有人請爲什麼我收到此錯誤,以及如何得到的圖片到我的電腦。

回答

1

BeautifulSoup是html/xml解析的庫。 在這個網址上你已經收到圖片了,那麼你想要解析什麼? 這工作正常:urllib.request.urlretrieve("http://images.nga.gov/?service=asset&action=show_preview&asset=33643" ,"C:\art.jpg")

0

有沒有必要使用BeautifulSoup!只要做到:

with urllib.request.urlopen("http://images.nga.gov/?service=asset&action=show_preview&asset=33643") as url: 
    s = url.read() 
with open("art.jpg", 'wb') as fp: 
    fp.write(url.read())