How to save pictures from a website to a local folder

前端 未结 3 536
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-21 12:28

I\'d need to save pictures from this website in a folder:

http://www.photobirdireland.com/garden-birds.html

I\'ve tried by using import os

from l         


        
3条回答
  •  无人及你
    2021-01-21 12:52

    Try following code to download images.use urlretrieve to download image src value to location.

    from urllib.request import urlretrieve
    import requests
    from bs4 import BeautifulSoup
    import os
    url='http://www.photobirdireland.com/garden-birds.html'
    data=requests.get(url).text
    soup=BeautifulSoup(data,"html.parser")
    images=['http://www.photobirdireland.com/'+ image['src'] for image in soup.find_all('img')]
    
    for img in images:
        urlretrieve(img,os.path.basename(img)) 
    

提交回复
热议问题