如何使用 python 和 BeautifulSoup 获取标签内的文本

时间:2021-07-05 17:08:10

标签: python beautifulsoup

我正在尝试使用漂亮的汤在标签内获取文本(示例文本)
html 结构如下所示:

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

我尝试了什么:

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

4 个答案:

答案 0 :(得分:1)

您不能将 .find() 与多个标签名称这样堆叠在一起使用。您需要反复调用 .find() 才能获得所需的结果。查看 docs 了解更多信息。下面的代码将为您提供所需的输出:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您的选择器有误。

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

答案 3 :(得分:0)

看看这个 -

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

text = soup.span.get_text()
print(text)
相关问题