1

Well, first of all I am not sure if it is an atrribute. If it is not, please tell me the right name. The page has a div tag with the general format:

<div class="top" data-src="/a/path/to/another/page.html" id="id_random" style="position: absolute; left: 0px; top: 0px;">

I want to retrieve the value of 'data-src' attribute (?) above, which is "/a/path/to/another/page.html", using python/bs4.

I already have the html code above in a variable named target_div and I have tried things like:

target_div.find(id='data-src')

or

target_div.find ('meta', {'name':'data-src'}) 

or

target_div.find (attrs={'name' : 'data-src'})

Please, how to find "data-src" and its value inside a div ?

best regards,

game0ver
  • 1,250
  • 9
  • 22
Miguel Rozsas
  • 397
  • 2
  • 3
  • 9
  • Possible duplicate of https://stackoverflow.com/questions/2612548/extracting-an-attribute-value-with-beautifulsoup? – inkychris Jul 16 '18 at 23:11

1 Answers1

2

You can try with this:

from bs4 import BeautifulSoup

target_div = '''<div class="top" data-src="/a/path/to/another/page.html" id="id_random" style="position: absolute; left: 0px; top: 0px;">'''

soup = BeautifulSoup(target_div, 'html.parser')
print soup.find('div', class_='top')['data-src']

The result should be:

/a/path/to/another/page.html
game0ver
  • 1,250
  • 9
  • 22