How to parse "data-text" with beautifulsoup?

Viewed 532

There is a part of the HTML code:

<div class="some div name" data-text="important text">...</div> 

We need to get the text from "data-text". I was trying to find something in the BeautifulSoup official documentation, but there is nothing like this (or I was looking badly).

4 Answers

You can use ['data-text'] or .get('data-text') on tag to get attribute value.

For example:

from bs4 import BeautifulSoup

txt = '''<div class="some div name" data-text="important text">...</div>'''
soup = BeautifulSoup(txt, 'html.parser')

print(soup.find('div', {'data-text': True})['data-text'])

Prints:

important text

You can try this one.

from bs4 import BeautifulSoup
import requests
...
bsObj = BeautifulSoup(html, features = "html.parser")
div_tag = bsObj.find("div", class_ = "some div name")
if div_tag:
    data_text = div_tag['data-text']
print(data_text)

Hope it could help you.

With so little information you provide, I can not come up with anything better than this:

from bs4 import BeautifulSoup

html = '<div class="some div name" data-text="important text">...</div>'
soup = BeautifulSoup(html, 'html.parser')
div = soup.select_one('div.some.div.name')
print(div.get('data-text'))

Output:

important text
Related