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

时间:2020-03-30 19:40:29

标签: python beautifulsoup

我只想从此处打印文本。

这是我的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/WebScrap/webscrap.py", line 8, in <module>
print(synonyms2[0].find(class_='short').get_text())
AttributeError: 'NoneType' object has no attribute 'get_text'

在我的输出中,我成功打印了与html标记关联的类值,但是当我尝试仅使用此行调用文本时

print(synonyms2[0].find(class_='short').get_text())

这是我的错误

 Traceback (most recent call last):
 File "/home/hudacse6/WebScrap/webscrap.py", line 8, in <module>
 print(synonyms2[0].find(class_='short').get_text())
 AttributeError: 'NoneType' object has no attribute 'get_text'. 

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

1 个答案:

答案 0 :(得分:4)

由于synonyms2[0].find(class_='short')返回None,所以您收到错误消息。

改为使用此:

代码

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].get_text())

输出

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.
相关问题