猿问

如何使用python beautifulsoup从html中提取文本

我有一个网页上的以下文字:


<dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term</df> 

</span>Here is the meaning of my term and its description; (<span 

class="TermLink">définition</span>)</p></dd>

<dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term 

2</df></span>Here is the meaning of my term 2 and its description; (<span 

class="TermLink">définition</span>)</p></dd>

<dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term 

3</df></span>Here is the meaning of my term 3 and its description; (<span 

class="TermLink">définition</span>)</p></dd>

我正在尝试使用 python beautifulsoup 库来提取定义术语,例如“示例术语”,然后是它的描述。


因此,我想看到:“示例术语”、“这是我的术语及其描述的含义”“示例术语 2”、“这是我的术语 2 的含义及其描述”“示例术语 3”、“这是我的 term3 的含义及其描述”


慕尼黑的夜晚无繁华
浏览 294回答 1
1回答

PIPIONE

html = '''<dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term</df> </span>Here is the meaning of my term and its description; (<span class="TermLink">définition</span>)</p></dd><dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term 2</df></span>Here is the meaning of my term 2 and its description; (<span class="TermLink">définition</span>)</p></dd><dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term 3</df></span>Here is the meaning of my term 3 and its description; (<span class="TermLink">définition</span></p></dd>'''soup = BeautifulSoup(html, 'html.parser')for each in soup.findAll('p', class_='Definition'):&nbsp; &nbsp; print(each.get_text())`
随时随地看视频慕课网APP

相关分类

Python
我要回答