Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download images from BeautifulSoup?

Image http://i.imgur.com/OigSBjF.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.

like image 556
Fist Heart Avatar asked May 11 '16 09:05

Fist Heart


1 Answers

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)
like image 124
Padraic Cunningham Avatar answered Sep 18 '22 10:09

Padraic Cunningham