sword1st
sword1st

Reputation: 555

Python 3 - Scrape Inner Div's Info with BS4

I'm trying to scrape some website with beautifulsoup but i couldn't make it work. On the site there is main div i'm taking that div with this code :

for divs in soup.find_all('div', {'class': 'row div-1'}): #I'm taking main div

     innerDivs = divs.find_all('div') #Taking inner div's in main div

     for inner in innerDivs: #loop for all inner divs

          print(inner) # I can print every inner div so it's working

Here one inner div structure :

<div class="inner-div preset multi">
<img class="img-resp high" data- 
src="https://image.test.co/skin/54asd15q1we12as1d1q/png.png" 
title="New Skin" width="100%"/>
</div>

I want to acces "title" and image-src but i don't know how. I tried 'inner.title' etc but i failed. Thanks for suggestions!

Upvotes: 0

Views: 317

Answers (1)

myusko
myusko

Reputation: 897

First, you need to find all <img> tags, and then just get their attributes, in your case it's ['title'] attribute.

In your example, you has only the one image tag, so you able to get him via list index [0], and then get his attributes.

from bs4 import BeautifulSoup


template = """
<div class="inner-div preset multi">
<img class="img-resp high" data- 
src="https://image.test.co/skin/54asd15q1we12as1d1q/png.png" 
title="New Skin" width="100%"/>
</div>
"""

source = BeautifulSoup(template, 'html.parser')

images = source.find_all('img')
print(images[0]['title']) # New skin
print(images[0]['src']) # https://image.test.co/skin/54asd15q1we12as1d1q/png.png
print(images[0]['width']) # 100%

Upvotes: 1

Related Questions