2016-02-27 52 views
0

現在面對一個奇怪的問題。 我嘗試了很多方法,但仍然無法解決問題。 我有以下基於類的視圖,它有一個get_context_data()並將處理給定城市的天氣。並從返回的JSON我需要提取正確的信息,我想使用,但是,我正在努力使其正確完成。 當我使用django-debug-toolbar檢查我的模板上下文呈現時,我能夠看到來自json的所有數據,但是當我在瀏覽器中的實際模板中時,我看到了一些奇怪的事情。 這裏是我的代碼:使用beautifulsoup和請求提取json數據

class FetchWeather(generic.TemplateView): 
    template_name = 'forecastApp/pages/weather.html' 

    request_post = None 

    def get_context_data(self, **kwargs): 
     context = super().get_context_data(**kwargs) 
     url = 'http://weather.news24.com/sa/cape-town' 
     city = 'cape town' 
     url_request = requests.get(url) 
     soup = BeautifulSoup(url_request.content, 'html.parser') 
     city_list = soup.find(id="ctl00_WeatherContentHolder_ddlCity") 
     print(soup.head) 
     city_as_on_website = city_list.find(text=re.compile(city, re.I)).parent 
     cityId = city_as_on_website['value'] 
     json_url = "http://weather.news24.com/ajaxpro/TwentyFour.Weather.Web.Ajax,App_Code.ashx" 

     headers = { 
      'Content-Type': 'text/plain; charset=UTF-8', 
      'Host': 'weather.news24.com', 
      'Origin': 'http://weather.news24.com', 
      'Referer': url, 
      'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/48.0.2564.82 Chrome/48.0.2564.82 Safari/537.36', 
      'X-AjaxPro-Method': 'GetCurrentOne'} 

     payload = { 
      "cityId": cityId 
     } 
     request_post = requests.post(json_url, headers=headers, data=json.dumps(payload)) 
     print(request_post.content) 
     data = re.sub(r"new Date\(Date\.UTC\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)\)", convert_date, request_post.text) 
     data = data.strip(";/*") 
     data = json.loads(data) 
     context["cityId"] = data 
     return context 

,而這些都是實際的模板的屏幕截圖,並從DEUG工具欄裏的分析: normal template view[![debug toolbar] 2

所以實際上,我只需要形成這樣的JSON在預測:

{ 
    'CountryName': 'South Africa', 
    '__type': 'TwentyFour.Services.Weather.Objects.CurrentOneReport, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null', 
    'MarineReport': None, 
    'TimeZone': '2', 
    'Location': { 
    '__type': 'TwentyFour.Services.Weather.Objects.Location, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null', 
    'Forecasts': [ 
     { 
     'DayLight': 'D', 
     'WindDirection': '161', 
     '__type': 'TwentyFour.Services.Weather.Objects.Forecast, TwentyFour.Services.Weather, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null', 
     'SkyDescriptor': '1', 
     'Date': '2016-01-27T22:00:00', 
     'Rainfall': '*', 
     'Icon': '1', 
     'WindDirectionDescription': 'South', 
     'Visibility': None, 
     'TemperatureDescription': 'Mild', 
     'HighTemp': '24', 
     'TemperatureDescriptor': '8', 
     'BeaufortDescriptor': 'Fresh breeze', 
     'Cached': False, 
     'PrecipitationDescriptor': '', 
     'Snowfall': '*', 
     'DaySegment': None, 
     'ShortWeekDay': 'Sun', 
     'DaySequence': 1, 
     'WindSpeed': '34', 
     'WeekDay': 'Sunday', 
     'Sky': 'Sunny', 
     'PrecipitationProbability': '0', 
     'Precipitation': '', 
     'WindDirectionAbreviated': 'S', 
     'FormattedDate': 'Sun, Feb 28', 
     'Segment': None, 
     'Beaufort': '5', 
     'Description': 'Sunny. Mild.', 
     'IconName': 'sunny', 
     'Temperature': None, 
     'DewPoint': '14', 
     'Air': 'Breezy', 
     'Humidity': '55', 
     'UV': 'High', 
     'Comfort': '25', 
     'LowTemp': '18', 
     'DayOfWeek': 1, 
     'AirDescription': '13' 
     } 
    ], 
    'City': '77107', 
    'Cached': False, 
    'CityName': 'Cape Town' 
    } 

是提取低溫,高溫和日期,使用beautifulsoup

+3

不完全理解你的問題。如果數據是json格式,爲什麼你需要BeautifulSoup? –

+0

我有一個非常類似的問題,我改用python鵝代替 – winixxee

+0

也許我使用了錯誤的方法,但是我希望能夠從使用beautifulsoup @warmoverflow後提取的json提取數據 –

回答

1

在我看來,美麗的湯用於檢索相關城市的city_id

一旦JSON已經檢索它被轉換成一個Python對象,具有:

... 
    data = json.loads(data) 
    ... 

假設這正確工作所需的項目可被挑選出來此對象的,並添加到上下文:

EDITED

.... 
    forecast = data['Forecast'] 
    context["LowTemp"] = forecast["LowTemp"] 
    context["HighTemp"] = forecast["HighTemp"] 
    context["Date"] = forecast["Date"] 
    return context 
+0

謝謝我嘗試了你的方式,但我有這個錯誤:文件「/home/drcongo/django_lab/weatherwebapp/src/forecastApp/views.py」,第59行,在get_context_data forecast = data ['cityId' ] ['預測'] –

+0

屏幕截圖和粘貼的json之間有一些差異。 '預測=數據''預測''工作嗎?屏幕截圖沒有最後的'預測'。 –

+0

是的,謝謝剛剛刪除了['cityId']但仍然對這個場景感到困惑,因爲json附帶了很多城市,卻無法真正瞭解爲什麼預測= data ['Forecast']返回了正確的數據在許多城市中沒有實際傳遞所需的城市ID –