2016-07-19 19 views
4

Ich bin sehr neu in Python und habe Probleme mit dem Code unten. Ich versuche, entweder die Temperatur oder das Datum auf der Website zu erhalten, kann aber keine Ausgabe bekommen. Ich habe viele Varianten ausprobiert, aber immer noch scheint es nicht richtig zu sein ..Probleme mit BeautifulSoup in Python

Vielen Dank für Ihre Hilfe!

#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

Verwenden Sie '.' als CSS-Begrenzer anstelle von Leerzeichen. Es sieht auch so aus, als hätten sie ihre IDs und Klassen geändert –

+0

Verwenden von "." scheint auch nicht zu funktionieren. Es gibt ziemlich viele (vielleicht 10) der 'fnd_date's, ich habe nichts dagegen, sie alle als eine Liste oder nur die ersten 1 Versucht, ein' für 'zu tun ... but..didn Es scheint auch nicht zu funktionieren. – Joe

Antwort

1

Ihr CSS-Selektor ist falsch, Sie . zwischen dem Tag-und CSS-Klassen verwenden sollten die Tags, die Sie wollen, sind in den divs mit der fnd_day Klasse innerhalb des div mit der ID fnd_content

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

Aber das wird noch nicht die Daten erhalten, wie es dynamisch generiert throug h eine Ajax-Anforderung, können Sie alle Daten in json Format erhalten den Code mit unter:

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) 

Das ist alles wieder so ziemlich den dynamischen Inhalt einschließlich der Termine und temps etc ..

Wenn Sie Zugriff auf den Schlüssel F9D, können Sie die allgemeine Wetterbeschreibung alle temps und Termine finden Sie unter:

from pprint import pprint as pp 

pp(data['F9D']) 

Ausgang:

{'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'}]} 

Die einzige Abfrage-String-Parameter ist der epoch timestamp, die Sie erzeugen können die Zeit lib mit:

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

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

nicht vorbei der Zeitstempel auch die gleichen Daten zurückgibt, damit ich Sie verlassen die Bedeutung zu untersuchen.

0

Ich war in der Lage, die Daten zu erhalten:

>>> 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>] 

Aber der Text fehlt. Dies scheint nicht ein Problem mit BeautifulSoup, weil ich selbst durch r.text sah und alles, was ich sehe, ist <div class="fnd_date"></div> anstelle von irgendetwas wie <div class="fnd_date">July 20</div>.

Sie können prüfen, ob der Text wird nicht regex (obwohl mit regex mit HTML ist verpönt):

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