如何在Python中使用Beautifulsoup仅打印文本?

如何在Python中使用Beautifulsoup仅打印文本?,python,beautifulsoup,Python,Beautifulsoup,我只想打印这里的文本 这里是我的HTML.Purser代码 import requests from bs4 import BeautifulSoup page = requests.get('https

我只想打印这里的文本

这里是我的HTML.Purser代码

import requests                                                  
from bs4 import BeautifulSoup                                    
                                                             
page = requests.get('https://www.vocabulary.com/dictionary/abet')
soup = BeautifulSoup(page.content, 'html.parser')                    
synonyms2 = soup.find_all(class_='short')                            
print(synonyms2[0])                                              
print(synonyms2[0].find(class_='short').get_text())   
输出

<p class="short">To <i>abet</i> is to help someone do something, usually something wrong. If 
you were the lookout while your older sister swiped cookies from the cookie jar, you 
<i>abetted</i> her mischief.</p>

Traceback (most recent call last):
File "/home/hudacse6/WebScrape/webscrape.py", line 8, in <module>
print(synonyms2[0].find(class_='short').get_text())
AttributeError: 'NoneType' object has no attribute 'get_text'
它会告诉我这个错误

 Traceback (most recent call last):
 File "/home/hudacse6/WebScrape/webscrape.py", line 8, in <module>
 print(synonyms2[0].find(class_='short').get_text())
 AttributeError: 'NoneType' object has no attribute 'get_text'. 
回溯(最近一次呼叫最后一次):
文件“/home/hudacse6/WebScrape/WebScrape.py”,第8行,在
打印(同义词2[0]。查找(class='short')。获取文本()
AttributeError:“非类型”对象没有属性“获取文本”。

如何避免此错误并仅打印文本。

您之所以会遇到此错误,是因为
同义词2[0]。find(class='short')
返回
None

改用这个:

代码

导入请求
从bs4导入BeautifulSoup
page=requests.get('https://www.vocabulary.com/dictionary/abet')
soup=BeautifulSoup(page.content,'html.parser')
同义词2=汤。查找所有(class='short')
打印(同义词2[0]。获取文本()
输出

To abet is to help someone do something, usually something wrong. If you were the lookout while your older sister swiped cookies from the cookie jar, you abetted her mischief.

同义词2已经是您从
find\u all(class='short')
获得的内容。同义词2[0]。get_text()会工作吗?对不起,我不明白你的意思。请不要介意。谢谢你,它管用,谢谢你much@c4llmeco4ch.I给你一个答案。这也是@c4llmeco4ch的意思。如果您对答案感到满意,请接受。谢谢
To abet is to help someone do something, usually something wrong. If you were the lookout while your older sister swiped cookies from the cookie jar, you abetted her mischief.