Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 地下天气预报_Python_Json_Api_Parsing_Web Scraping - Fatal编程技术网

Python 地下天气预报

Python 地下天气预报,python,json,api,parsing,web-scraping,Python,Json,Api,Parsing,Web Scraping,我正在尝试按天分离天气api的代码。目前,我可以获取forecastday的数组,但无法获取fcttext和title的每个数组。以下是我迄今为止用python编写的代码和我想要提取的数据: import requests from pprint import pprint import json r = requests.get("http://api.wunderground.com/api/id/forecast10day/q/CA/San_Francisco.json") data =

我正在尝试按天分离天气api的代码。目前,我可以获取forecastday的数组,但无法获取fcttext和title的每个数组。以下是我迄今为止用python编写的代码和我想要提取的数据:

import requests
from pprint import pprint
import json

r = requests.get("http://api.wunderground.com/api/id/forecast10day/q/CA/San_Francisco.json")
data = r.json()
pprint (data['forecast']['txt_forecast']['forecastday'])


"forecastday": [
    {
    "period":0,
    "icon":"partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
    "title":"Thursday",
    "fcttext":"Cloudy early with peeks of sunshine expected late. High 54F. Winds W at 10 to 20 mph.",
    "fcttext_metric":"Cloudy skies early, then partly cloudy this afternoon. High 12C. Winds W at 15 to 30 km/h.",
    "pop":"0"
    }
    ,
    {
    "period":1,
    "icon":"nt_mostlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/nt_mostlycloudy.gif",
    "title":"Thursday Night",
    "fcttext":"Partly cloudy this evening, then becoming cloudy after midnight. Low 38F. Winds N at 5 to 10 mph.",
    "fcttext_metric":"Partly cloudy early followed by cloudy skies overnight. Low 3C. Winds N at 10 to 15 km/h.",
    "pop":"0"
    }
    ,
    {
    "period":2,
    "icon":"chancerain",
    "icon_url":"http://icons.wxug.com/i/c/k/chancerain.gif",
    "title":"Friday",
    "fcttext":"Light rain early...then remaining cloudy with showers in the afternoon. High 52F. Winds NE at 10 to 15 mph. Chance of rain 60%.",
    "fcttext_metric":"Light rain early...then remaining cloudy with showers in the afternoon. High 11C. Winds NE at 15 to 25 km/h. Chance of rain 60%.",
    "pop":"60"
    }

data['forecast']['txt\u forecast']['forecastday']
中的值是字典列表。因此,您需要在该列表上循环以从中取出项目

for forecastday in data['forecast']['txt_forecast']['forecastday']:
    print(forecastday['title'])
    print(forecastday['fcttext'])
产生:

Thursday
Cloudy early with peeks of sunshine expected late. High 54F. Winds W at 10 to 20 mph.
etc...

谢谢您!工作完美。