2016-07-19 3 views
4

私は非常にPythonに新しく、以下のコードで問題があります。ウェブサイト上で温度や日付を取得しようとしていますが、出力が得られないようです。私は多くのバリエーションを試しましたが、それでも正しいように見えません。pythonでbeautifulsoupに問題があります

ありがとうございました!

#Code below: 
import requests,bs4 
r = requests.get('http://www.hko.gov.hk/contente.htm') 
print r.raise_for_status() 
hkweather = bs4.BeautifulSoup(r.text) 
print hkweather.select('div left_content fnd_day fnd_date') 
+0

スペースの代わりにcss区切り記号として '.'を使用してみてください。彼らは彼らのidsとクラスを変更したようにも見える –

+0

"。"どちらもうまくいかないようです。 'fnd_date'のかなりの数(多分10個)があります。リストとして、あるいは最初の1つをすべて持っていても構いません。 'のために'しようとしました... but..didnどちらもうまくいくようです。 – Joe

答えて

1

あなたのCSSセレクタが間違っている、あなたは、タグやCSSクラス間.を使用する必要があります、あなたがしたいタグがidを持つdivの内側fnd_dayクラスfnd_content

divs = soup.select("#fnd_content div.fnd_day") 

とのdivである。しかし、それは、動的に生成されthrougとしてそれはまだデータを取得することはありません

あなたの場合...日付やタンなどを含めたほとんどすべての動的コンテンツを返す

u = "http://www.hko.gov.hk/wxinfo/json/one_json.xml?_=1468955579991" 

data = requests.get(u).json() 

from pprint import pprint as pp 
pp(data) 

:H AJAX要求は、あなたは以下のコードを使用してJSONでフォーマットをすべてのデータを取得することができますキーF9Dにアクセスするには、一般的な天候の説明を見ることができるすべてのタンと日付:

from pprint import pprint as pp 

pp(data['F9D']) 

出力:

{'BulletinDate': '20160720', 
'BulletinTime': '0315', 
'GeneralSituation': 'A southwesterly airstream will bring showers to the ' 
        'coast of Guangdong today. Under the dominance of an ' 
        'upper-air anticyclone, it will be generally fine and ' 
        'very hot over southern China in the latter part of this ' 
        'week and early next week.', 
'NPTemp': '25', 
'WeatherForecast': [{'ForecastDate': '20160720', 
         'ForecastIcon': 'pic53.png', 
         'ForecastMaxrh': '95', 
         'ForecastMaxtemp': '32', 
         'ForecastMinrh': '70', 
         'ForecastMintemp': '26', 
         'ForecastWeather': 'Sunny periods and a few showers. ' 
             'Isolated squally thunderstorms at ' 
             'first.', 
         'ForecastWind': 'South to southwest force 4.', 
         'IconDesc': 'Sunny Periods with A Few Showers', 
         'WeekDay': '3'}, 
        {'ForecastDate': '20160721', 
         'ForecastIcon': 'pic90.png', 
         'ForecastMaxrh': '90', 
         'ForecastMaxtemp': '33', 
         'ForecastMinrh': '65', 
         'ForecastMintemp': '28', 
         'ForecastWeather': 'Mainly fine and very hot apart from ' 
             'isolated showers in the morning.', 
         'ForecastWind': 'South to southwest force 3 to 4.', 
         'IconDesc': 'Hot', 
         'WeekDay': '4'}, 
        {'ForecastDate': '20160722', 
         'ForecastIcon': 'pic90.png', 
         'ForecastMaxrh': '90', 
         'ForecastMaxtemp': '33', 
         'ForecastMinrh': '65', 
         'ForecastMintemp': '28', 
         'ForecastWeather': 'Mainly fine and very hot apart from ' 
             'isolated showers in the morning.', 
         'ForecastWind': 'Southwest force 3.', 
         'IconDesc': 'Hot', 
         'WeekDay': '5'}, 
        {'ForecastDate': '20160723', 
         'ForecastIcon': 'pic90.png', 
         'ForecastMaxrh': '90', 
         'ForecastMaxtemp': '34', 
         'ForecastMinrh': '65', 
         'ForecastMintemp': '29', 
         'ForecastWeather': 'Fine and very hot.', 
         'ForecastWind': 'Southwest force 3.', 
         'IconDesc': 'Hot', 
         'WeekDay': '6'}, 
        {'ForecastDate': '20160724', 
         'ForecastIcon': 'pic90.png', 
         'ForecastMaxrh': '90', 
         'ForecastMaxtemp': '34', 
         'ForecastMinrh': '65', 
         'ForecastMintemp': '29', 
         'ForecastWeather': 'Fine and very hot.', 
         'ForecastWind': 'Southwest force 3.', 
         'IconDesc': 'Hot', 
         'WeekDay': '0'}, 
        {'ForecastDate': '20160725', 
         'ForecastIcon': 'pic90.png', 
         'ForecastMaxrh': '90', 
         'ForecastMaxtemp': '33', 
         'ForecastMinrh': '65', 
         'ForecastMintemp': '29', 
         'ForecastWeather': 'Mainly fine and very hot apart from ' 
             'isolated showers in the morning.', 
         'ForecastWind': 'South to southwest force 3.', 
         'IconDesc': 'Hot', 
         'WeekDay': '1'}, 
        {'ForecastDate': '20160726', 
         'ForecastIcon': 'pic90.png', 
         'ForecastMaxrh': '90', 
         'ForecastMaxtemp': '33', 
         'ForecastMinrh': '65', 
         'ForecastMintemp': '29', 
         'ForecastWeather': 'Mainly fine and very hot apart from ' 
             'isolated showers in the morning.', 
         'ForecastWind': 'South to southwest force 3.', 
         'IconDesc': 'Hot', 
         'WeekDay': '2'}, 
        {'ForecastDate': '20160727', 
         'ForecastIcon': 'pic90.png', 
         'ForecastMaxrh': '90', 
         'ForecastMaxtemp': '33', 
         'ForecastMinrh': '65', 
         'ForecastMintemp': '28', 
         'ForecastWeather': 'Mainly fine and very hot apart from ' 
             'isolated showers in the morning.', 
         'ForecastWind': 'Southwest force 3 to 4.', 
         'IconDesc': 'Hot', 
         'WeekDay': '3'}, 
        {'ForecastDate': '20160728', 
         'ForecastIcon': 'pic90.png', 
         'ForecastMaxrh': '90', 
         'ForecastMaxtemp': '33', 
         'ForecastMinrh': '65', 
         'ForecastMintemp': '28', 
         'ForecastWeather': 'Mainly fine and very hot apart from ' 
             'isolated showers in the morning.', 
         'ForecastWind': 'Southwest force 3 to 4.', 
         'IconDesc': 'Hot', 
         'WeekDay': '4'}]} 

のみクエリ文字列パラメータを使用すると、時間のlibを使用して生成することができepoch timestampです:私は重要性を調査するためにあなたを残しますので

from time import time 
u = "http://www.hko.gov.hk/wxinfo/json/one_json.xml?_={}".format(int(time())) 

data = requests.get(u).json() 

もタイムスタンプを渡すことなく、同じデータを返します。

0

私は日付を取得することができた:

>>> import requests,bs4 
>>> r = requests.get('http://www.hko.gov.hk/contente.htm') 
>>> hkweather = bs4.BeautifulSoup(r.text) 
>>> print hkweather.select('div[class="fnd_date"]') 
# [<div class="fnd_date"></div>, <div class="fnd_date"></div>, <div class="fnd_date"></div>, <div class="fnd_date"></div>, <div class="fnd_date"></div>, <div class="fnd_date"></div>, <div class="fnd_date"></div>, <div class="fnd_date"></div>, <div class="fnd_date"></div>] 

をしかし、テキストがありません。私がr.text自分自身を見て、私が見ているのは<div class="fnd_date"></div>で、<div class="fnd_date">July 20</div>のようなものではなく、これはBeautifulSoupの問題のようには見えません。

テキストは、(HTMLに正規表現を使用しては眉をひそめているが)正規表現を使用して存在しないことを確認することができます。

>>> import re 
>>> re.findall(r'<[^<>]*fnd_date[^<>]*>[^>]*>', r.text) 
# [u'<div id="fnd_date" class="date"></div>', ... repeated 10 times] 
関連する問題