# -*- coding: utf-8 -*-
# requirements
import requests, termcolor, html2text
import os, urllib2
try:
from bs4 import BeautifulSoup
except:
import BeautifulSoup
html1 ="""
<div class="zm-item-answer-author-info"><span class="name">匿名用户</span></div>
"""
html2="""
<div class="zm-item-answer-author-info">
<span class="name">匿名用户</span>
</div>
"""
soup1 = BeautifulSoup(html1)
print soup1.find_all("div", class_="zm-item-answer-author-info")[0].string
# 匿名用户
print soup1.find_all("div", class_="zm-item-answer-author-info")[0].text
# 匿名用户
soup2 = BeautifulSoup(html2)
print soup2.find_all("div", class_="zm-item-answer-author-info")[0].string
# None
print soup2.find_all("div", class_="zm-item-answer-author-info")[0].text.strip()
# 匿名用户
Beautiful Soup问题汇总
最新推荐文章于 2020-09-27 00:52:09 发布