samedi 7 octobre 2017

Scraping images that were loaded locally

I'm working on learning Beautiful Soup and am running into an issue while trying to scrape an image that was uploaded from a local directory. The error I am seeing is:

ValueError: unknown url type: 'images/ixa2.png'

What I assume is happening is the image was loaded from a local directory and isn't being hosted through a URL. This is what it looks like when I inspect the element that I am attempting to scrape:

<img width="200" align="left" hspace="0" src="ixa/cards/axisofmortality.jpg">

Here's the code I am using:

from urllib import request
import urllib.request
from bs4 import BeautifulSoup as soup

def make_soup(url):
    result = request.urlopen(url)
    page = result.read()

    parsed_page = soup(page, "html.parser")
    result.close()
    return parsed_page

def get_images(url):
    soup = make_soup(url)
    images = [img for img in soup.findAll('img')]
    print (str(len(images)) + "images found.")
    print('Downloading images to current working directory.')
    #compile our unicode list of image links
    image_links = [each.get('src') for each in images]
    for each in image_links:
        filename=each.split('/')[-1]
        urllib.request.urlretrieve(each, filename)
    return image_links

get_images('http://ift.tt/1iqkhK2')

Aucun commentaire:

Enregistrer un commentaire