akshay_rahar akshay_rahar - 3 months ago 17
Python Question

How to get the image src data through beautifulsoup?

I want to get the image src data of all

coming soon
movies from this link:-
Fandango.com

This is the code:-

def poster(genre):
poster_link = []
request = requests.get(http://www.fandango.com/moviescomingsoon?GenreFilter=genre)
content = request.content
soup = BeautifulSoup(content, "html.parser")
soup2 = soup.find('div', {'class':'movie-ls-group'})
elements = soup2.find_all('img')

for element in elements:
poster_link.append(element.get('src'))

return poster_link


When I'm printing the poster_link array then it's giving me
None
instead of image source.

Answer

Try this. It shortcuts the subsetting and grabs all of the images that have the proper class.

def poster(genre):
    poster_link = []
    request = requests.get('http://www.fandango.com/moviescomingsoon?GenreFilter=%s' %genre)
    content = request.content
    soup = BeautifulSoup(content, "html.parser")
    imgs = soup.find_all('img', {'class': 'visual-thumb'})

    for img in imgs:
        poster_link.append(img.get('data-src'))
    return poster_link
Comments