2016-04-26 51 views
0

我想創建一個python圖像抓取工具。獲取imgae src並通過python圖像抓取工具將圖像保存到目錄中

這是我現在有:

from bs4 import BeautifulSoup 
from urllib.request import urlopen 
url = 'http://blog.pouyacode.net/' 
data = urlopen(url) 
soup = BeautifulSoup(data, 'html.parser') 
img = soup.findAll('img') 
print (img) 
print ('\n') 
print ('****************************') 
print ('\n') 
for each in img: 
    print(img.get('src')) 
    print ('\n') 

這部分工作:

print (img) 
print ('\n') 
print ('****************************') 
print ('\n') 

但在輸出*****************後,會出現這樣的錯誤:

Traceback (most recent call last): 
File "pull.py", line 15, in <module> 
print(img.get('src')) 
AttributeError: 'ResultSet' object has no attribute 'get' 

因此,如何能我得到所有圖像的所有SRC? 如何將這些圖像保存在目錄中?

+1

你可能意味着使用each.get(「SRC」),而不是img.get(「SRC」) – Zillolo

+0

是的,對不起,這是一個小錯誤!謝謝。但是第二個,將圖像保存在文件夾中呢? – niloofar

回答

2

這樣的事情?書面的頭腦和未測試

from bs4 import BeautifulSoup 
from urllib.request import urlopen 
import os 

url = 'http://blog.pouyacode.net/' 
download_folder = "downloads" 

if not os.path.exists(download_folder): 
    os.makedirs(download_folder) 

data = urlopen(url) 
soup = BeautifulSoup(data, 'html.parser') 
img = soup.findAll('img') 

for each in img: 
    url = each.get('src') 
    data = urlopen(url) 
    with open(os.path.join(download_folder, os.path.basename(url)), "wb") as f: 
     f.write(data.read()) 
+0

是的是的是的!!!!!!非常感謝你@salmonderossi :) – niloofar

+1

@niloofar不客氣。我清理了我的答案了一下... – salomonderossi

相關問題