AlphaWolf AlphaWolf - 7 months ago 29
Python Question

Python beautiful soup find content in class

I got a html like this:

<div class="row" data-keywordid="53707903">
<div class="kw-table-cell kw-keyword-name-cell">
</div>
</div>


In normal case i can take content inner of class "row" by using:
soup.find("div",{"class": "row"})

but what i want is get the
data-keywordid
and value of that
53707903
. I have no idea for it. Have anyway to get it? Thanks !

Answer Source
from bs4 import BeautifulSoup

text = ''''
    <div class="row" data-keywordid="53707903">
     <div class="kw-table-cell kw-keyword-name-cell">
     </div>
    </div>
'''

soup = BeautifulSoup(text, "html.parser")

a = soup.find('div')

a.attrs['data-keywordid']