Reputation: 169
I want to extract attribute from within a HTML tag using beautifulsoup. How to do it ?
For Example:
<div class="search-pagination-top clearfix mtop ">
<div class="row"><div class="col-l-4 mtop pagination-number" tabindex="0"
aria-label="Page 1 of 15 "><div>Page <b>1</b> of <b>15</b> </div></div>
How do I get text from "aria-label" attribute ?
I tried using select() but it didn't help.
Upvotes: 5
Views: 9824
Reputation: 82785
You can extract the attribute value just like a dictionary. Using the key aria-label
Ex:
from bs4 import BeautifulSoup
html = """<div class="search-pagination-top clearfix mtop ">
<div class="row"><div class="col-l-4 mtop pagination-number" tabindex="0"
aria-label="Page 1 of 15 "><div>Page <b>1</b> of <b>15</b> </div></div>
"""
soup = BeautifulSoup(html, "html.parser")
print( soup.find("div", class_="col-l-4 mtop pagination-number")["aria-label"] )
Output:
Page 1 of 15
Upvotes: 3