我需要使用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')
答案 0 :(得分:1)
使用以下代码可以正常工作 从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)
答案 1 :(得分: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 :(得分: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')
for tag in soup.find_all('div', attrs = {'class': 'msgdiv'}):
for xmp in tag.find_all('font'):
print(xmp.text)