Python 按字符串匹配子集bs4.element.ResultSet

Python 按字符串匹配子集bs4.element.ResultSet,python,beautifulsoup,Python,Beautifulsoup,我试图引用html中某个未命名的段落。我已识别出正确的,它存储在res中 我现在想做的是:找到,其中包含单词“Country”,并接收作为字符的值(here=“Germany”)。不幸的是,位置匹配不起作用,因为位置会随着页面的变化而变化 res=res.find(name=“div”,attrs={“class”:“项目结果文本”}) 打印(res) 结果: [<p><span>Country: </span>Germany</p>, <

我试图引用html中某个未命名的
段落。我已识别出正确的
,它存储在
res

我现在想做的是:找到
,其中
包含单词“Country”,并接收作为字符的值(here=“Germany”)。不幸的是,位置匹配不起作用,因为位置会随着页面的变化而变化

res=res.find(name=“div”,attrs={“class”:“项目结果文本”})
打印(res)
结果:

[<p><span>Country: </span>Germany</p>, <p><span>Coly: </span> Print</p>]

结果:

不过我会尝试用另一种方式。我不确定这是否正是你想要的

In [5]: a = "<p><span>Country: </span>Germany</p>, <p><span>Coly: </span> Print</p>"
In [6]: soup = BeautifulSoup(a,"lxml")
In [14]: values = {i.text.strip().split(":")[0].strip():i.text.strip().split(":")[1].strip() for i in soup.find_all("p")}

In [15]: values["Country"]
Out[15]: 'Germany'
[5]中的
:a=“国家:德国,Coly:Print

” 在[6]中:汤=美汤(a,“lxml”) 在[14]中:值={i.text.strip().split(“:”[0].strip():i.text.strip().split(“:”[1].strip()表示汤中的i。查找所有(“p”)} 在[15]中:值[“国家”] Out[15]:“德国”
谢谢。这是一个非常简洁的解决方案,我将把它添加到书签中。同时,我碰巧用
soup.find('span',text=re.compile(“^Country”).next\u sibling.string解决了这个问题