简体   繁体   中英

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))

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()
r = requests.get(url)
soup = bs(r.content, 'html.parser')
desc = soup.find('div').find('span')
print(desc.getText())

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'
>>>

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM