如何从P标签中提取标题

时间:2017-06-04 17:35:22

标签: python beautifulsoup

<p class="mrgn-bttm-0 pop text-center" title="Chance of Precipitation"><small> 30%</small></p>

我想从上面的示例p标签中提取“降低30%的可能性”。实际上有14个这样的标签,每天一个。我使用了所有的p标签;

f = soup.find('details', {'class':"panel panel-default wxo-fcst"})

然后我尝试过:

for i in f:
    print i.find('p')['title']

for i in x.findAll("p"):
    print i.find('p')['title'])  

但我无处可去。有人可以协助吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

f = soup.find('details', {'class':"panel panel-default wxo-fcst"})
for i in f.find_all('p',{'class':'mrgn-bttm-0 pop text-center'}):
    print i['title']+" "+i.get_text().strip()

输出:

Chance of Precipitation 
Chance of Precipitation 
Chance of Precipitation 
Chance of Precipitation 
Chance of Precipitation 
Chance of Precipitation 60%
Chance of Precipitation 
Chance of Precipitation 
Chance of Precipitation 
Chance of Precipitation 
Chance of Precipitation 
Chance of Precipitation 60%