2017-06-04 17 views
1
<p class="mrgn-bttm-0 pop text-center" title="Chance of Precipitation"><small> 30%</small></p> 

Ich möchte "Chance of Precipitation 30%" aus dem obigen Beispiel p-Tag extrahieren. Es gibt 14 dieser Tags, einen für jeden Tag. Ich habe alle p Tags mit;So extrahieren Sie Titel aus P-Tag

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

dann habe ich versucht:

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

und

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

aber ich bin immer nirgends. Kann mir bitte jemand helfen?

+1

Try '[p [ 'title'] + p.text.strip() für p in f.findAll ('p ')] '? – Psidom

+0

In Beispiel haben Sie Klasse = "Mrgn-Bttm-0 Pop-Text-Center", aber Sie zuweisen Variable "f" "Klasse": "Panel Panel-Standard Wxo-Fcst" – Zydnar

+0

mit Ihrem Vorschlag bekomme ich "Ausnahme ist 'Titel'". Ich versuche, die Daten von https://weather.gc.ca/city/pages/ab-52_metric_e.html zu scape. f = soup.find ('details', {'class': "panel panel-default wxo-fcst"}) gibt mir alle p takes, aus denen ich den titel abkratzen möchte. –

Antwort

0

auf diese Weise ausprobieren:

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() 

Ausgang:

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% 
Verwandte Themen