【问题标题】:How to get text from this cede using BeautifulSoup?如何使用 BeautifulSoup 从这个 cede 中获取文本?
【发布时间】:2018-08-17 08:39:33
【问题描述】:
我需要使用 bs4 获取此文本“请重新登录”
<div id="msgDiv" align="left" class="msgdiv">
<font class="errorfont">
<xmp style="white-space: normal;margin:0px;font-family:tahoma,arial,san-serif;">Please login again.</xmp>
</font>
我试过了,但没有得到任何东西:
page.select('#msgDiv > font > xmp')
【问题讨论】:
标签:
python
python-3.x
beautifulsoup
【解决方案1】:
这可能会有所帮助
from bs4 import BeautifulSoup
text = """
<div id="msgDiv" align="left" class="msgdiv">
<font class="errorfont">
<xmp style="white-space: normal;margin:0px;font-family:tahoma,arial,san-serif;">Please login again.</xmp>
</font>
"""
soup = BeautifulSoup(text, 'html.parser')
tag = soup.select('div#msgDiv > font.errorfont > xmp')[0]
print(tag.get_text())
【解决方案2】:
以下代码可以正常工作
从 bs4 导入 BeautifulSoup
text = """
<div id="msgDiv" align="left" class="msgdiv">
<font class="errorfont">
<xmp style="white-space: normal;margin:0px;font-family:tahoma,arial,san-serif;">Please login again.</xmp>
</font>
</div>
"""
soup = BeautifulSoup(text, "html.parser")
print(soup.xmp.text)
【解决方案3】:
这应该可以解决问题:
from bs4 import BeautifulSoup
text = """
<div id="msgDiv" align="left" class="msgdiv">
<font class="errorfont">
<xmp style="white-space: normal;margin:0px;font-family:tahoma,arial,san-serif;">Please login again.</xmp>
</font>
"""
soup = BeautifulSoup(text, 'html.parser')
for tag in soup.find_all('div', attrs = {'class': 'msgdiv'}):
for xmp in tag.find_all('font'):
print(xmp.text)