user16279183
user16279183

Reputation:

How to get text inside tag using python and BeautifulSoup

I'm trying to get text (example text) inside tags using beautiful soup
The html structure looks like this:

...
<div>
       <div>Description</div>
       <span>
          <div><span>example text</span></div>
       </span>
    </div>
...

What i tried:

r = requests.get(url)
soup = bs(r.content, 'html.parser')
desc = soup.find('div.div.span.div.span')
print(str(desc))

Upvotes: 0

Views: 92

Answers (4)

Tanish Sarmah
Tanish Sarmah

Reputation: 486

Check this out -

soup = BeautifulSoup('''<div>
       <div>Description</div>
       <span>
          <div><span>example text</span></div>
       </span>
    </div>''',"html.parser")

text = soup.span.get_text()
print(text)

Upvotes: 0

Isolated Gamer
Isolated Gamer

Reputation: 65

r = requests.get(url)
soup = bs(r.content, 'html.parser')
desc = soup.find('div').find('span')
print(desc.getText())

Upvotes: 0

Rustam Garayev
Rustam Garayev

Reputation: 2692

You cannot use .find() with multiple tag names in it stacked like this. You need to repeatedly call .find() to get desired result. Check out docs for more information. Below code will give you desired output:

soup.find('div').find('span').get_text()

Upvotes: 1

user5386938
user5386938

Reputation:

Your selector is wrong.

>>> from bs4 import BeautifulSoup
>>> data = '''\
... <div>
...        <div>Description</div>
...        <span>
...           <div><span>example text</span></div>
...        </span>
...     </div>'''
>>> soup = BeautifulSoup(data, 'html.parser')
>>> desc = soup.select_one('div span div span')
>>> desc.text
'example text'
>>>

Upvotes: 0

Related Questions