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

上記の例のpタグから "Chance of Precipitation 30%"を抽出します。これらのタグは実際には14個あり、毎日1個です。私はすべてのpタグを使用しています。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

てみ '[P [ 'タイトル'] + p.text.strip()f.findAllのpのために(P ' ')] '? – Psidom

+0

あなたはclass = "mrgn-bttm-0 pop text-center"を持っていますが、変数 "f" classに従っています: "panel panel-default wxo-fcst" – Zydnar

+0

あなたの提案を使用して "Exception is 'タイトル'"。私はhttps://weather.gc.ca/city/pages/ab-52_metric_e.htmlからのデータに触れようとしています。 f = soup.find( '詳細'、{'クラス': "パネルパネル - デフォルトwxo-fcst"})は、私がタイトルを掻きたいと思うすべてのpを与えます。 –

答えて

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% 
関連する問題