天气地下解析Python

时间:2017-04-27 15:35:08

标签: python json api parsing web-scraping

我正在尝试将天气地下api的代码分开。目前,我可以获取预测日的数组,但不能获得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"
    }

1 个答案:

答案 0 :(得分:1)

script.Parent.MouseButton1Click:connect(function(player) local BackPack = player.BackPack wait(0.5) if player.Leaderstats.Money >= 100 then local randomizer = math.random(0,10) if randomizer == 1 then local knife1 = game.Lighting.CookieKnife:Clone() knife1.Parent = BackPack elseif randomizer == 2 then local knife2 = game.Lighting.Cleaver:Clone() knife2.Parent = BackPack elseif randomizer == 3 then local knife3 = game.Lighting.RustyCleaver:Clone() knife3.Parent = BackPack elseif randomizer == 4 then local knife4 = game.Lighting.WhiteCleaver:Clone() knife4.Parent = BackPack elseif randomizer == 5 then local knife5 = game.Lighting["Sparkley Knife"]:Clone() knife5.Parent = BackPack elseif randomizer == 6 then local knife6 = game.Lighting.CommonKnife:Clone() knife6.Parent = BackPack elseif randomizer == 7 then local knife7 = game.Lighting.CommonKnife:Clone() knife7.Parent = BackPack elseif randomizer == 8 then local knife8 = game.Lighting.CookieKnife:Clone() knife8.Parent = BackPack elseif randomizer == 9 then local knife9 = game.Lighting.ExpensiveKnife:Clone() knife9.Parent = BackPack elseif randomizer == 10 then local knife10 = game.Lighting.CookieKnife:Clone() knife10.Parent = BackPack end end end) 中的值是字典列表。因此,您需要遍历该列表以从中获取项目。

data['forecast']['txt_forecast']['forecastday']

产地:

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