-3

この天気予報のウェブサイトから情報を取得するのに問題があります。私は主に「現在の状態」情報テーブルを取得しようとしています。ここに私がこれまでに持っているものがありますが、いくつかのエラーが発生しています。

import urllib2
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen('http://www.timeanddate.com/weather/usa/los-angeles').read())

for row in soup('table', {'class' : 'rpad'})[0].tbody('trtd'):
tds = row('tr')
print tds.string
4

1 に答える 1

0
#!/usr/bin/env python
import urllib2
from bs4 import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen('http://www.timeanddate.com/weather/usa/los-angeles').read())

for tr in soup.table.find_all('tr'):
    if tr('th'):
        if tr.th.string == "Current conditions":
            tds = tr('td')
            for td in tds:
                print td.string

私のソリューション、実行時の出力は次のようになります。

None
Location:
Los Angeles / USC Campus Downtown
Temperature:
63 °F
Comfort Level:
63 °F
Dew point:
51 °F
Pressure:
29.94 "Hg
Humidity:
65%
Visibility:
10 mi
Wind:
No wind
Last update:
Sun 7:47 PM PDT

疲れていたので、最初の出力の None チェックを追加するのに余分な時間はかかりませんでした。また、明らかなようにフォーマットはありませんが、それを自分で追加するのはそれほど難しくありません。

お役に立てば幸いです。

于 2012-05-28T03:52:32.257 に答える