Beautiful Soup问题汇总

来源:互联网 发布:西门子plc编程视频 编辑:程序博客网 时间:2024/06/01 16:47
# -*- coding: utf-8 -*-# requirementsimport requests, termcolor, html2textimport os, urllib2 try:    from bs4 import BeautifulSoupexcept:    import BeautifulSouphtml1 =""" <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# Noneprint soup2.find_all("div", class_="zm-item-answer-author-info")[0].text.strip()# 匿名用户
1 0