Reputation: 427
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,
Upvotes: 1
Views: 239
Reputation: 1290
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
Upvotes: 2