5

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.

1 Answer 1

3

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.