Search code examples
pythonpython-2.7web-scrapingbeautifulsoup

How to download images from BeautifulSoup?


Image https://i.sstatic.net/S1BR2.png

import requests from bs4 import BeautifulSoup

r = requests.get("xxxxxxxxx")
soup = BeautifulSoup(r.content)

for link in links:
    if "http" in link.get('src'):
       print link.get('src')

I get the printed URL but don't know how to work with it.


Solution

  • You need to download and write to disk:

    import requests
    from os.path  import basename
    
    r = requests.get("xxx")
    soup = BeautifulSoup(r.content)
    
    for link in links:
        if "http" in link.get('src'):
            lnk = link.get('src')
            with open(basename(lnk), "wb") as f:
                f.write(requests.get(lnk).content)
    

    You can also use a select to filter your tags to only get the ones with http links:

    for link in soup.select("img[src^=http]"):
            lnk = link["src"]
            with open(basename(lnk)," wb") as f:
                f.write(requests.get(lnk).content)