您的位置:首页 > 其它

Beautiful Soup问题汇总

2015-11-25 22:45 225 查看
# -*- 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()
# 匿名用户
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: