beautifulsoup判断是否是字符串对象
时间: 2023-06-23 16:59:55 浏览: 47
在使用beautifulsoup解析HTML等文档时,可以使用BeautifulSoup对象的find()或find_all()方法来查找特定的标签或文本内容。如果需要判断查找结果是否为字符串对象,可以使用Python内置的isinstance()函数来判断。
举个例子,假设我们有一个包含HTML文本的字符串,如下:
```
html_doc = "<html><head><title>The Dormouse's story</title></head><body><p class='title'><b>The Dormouse's story</b></p><p class='story'>Once upon a time there were three little sisters; and their names were <a href='http://example.com/elsie' class='sister' id='link1'>Elsie</a>, <a href='http://example.com/lacie' class='sister' id='link2'>Lacie</a> and <a href='http://example.com/tillie' class='sister' id='link3'>Tillie</a>; and they lived at the bottom of a well.</p></body></html>"
```
我们可以使用BeautifulSoup对象对其进行解析:
```
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
```
然后可以使用find()或find_all()方法查找特定的标签或文本内容,例如:
```
title = soup.find('title')
print(isinstance(title.string, str)) # True
links = soup.find_all('a')
for link in links:
print(isinstance(link.string, str)) # True
```
在上面的例子中,我们使用isinstance()函数判断查找结果的.string属性是否为字符串对象。如果是字符串对象,则返回True;否则返回False。