Python:在& lt//span& gt;之后提取文本< br/>之前 [英] Python:extract text after </span> before <br/>

查看:115
本文介绍了Python:在& lt//span& gt;之后提取文本< br/>之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要处理的html文件:

Here is the html file I am going to handle:

<span class="pl">Countries:</span> USA <br/>
<span class="pl">Language:</span> English <br/>

这是我的python代码:

And here is my python code:

from bs4 import BeautifulSoup

record=[]
soup=BeautifulSoup(html)
spans=soup.find_all('span')
for span in spans:
   record.append(span.text)

我最后得到的是:

Countries: Language:

结果缺少一些重要信息:美国"和英语"我如何获得短信?

The result miss some important information :"USA" and "English" How can I get the text?

推荐答案

使用

Use the .next_sibling notation:

soup.find("span", text="Countries:").next_sibling
soup.find("span", text="Language:").next_sibling

这篇关于Python:在&amp; lt//span&amp; gt;之后提取文本&lt; br/&gt;之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆