現在面對一個奇怪的問題。 我嘗試了很多方法,但仍然無法解決問題。 我有以下基於類的視圖,它有一個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工具欄裏的分析: ] 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
不完全理解你的問題。如果數據是json格式,爲什麼你需要BeautifulSoup? –
我有一個非常類似的問題,我改用python鵝代替 – winixxee
也許我使用了錯誤的方法,但是我希望能夠從使用beautifulsoup @warmoverflow後提取的json提取數據 –