【问题标题】:KeyError: 'data in one function but not in the othersKeyError: '数据在一个函数中,但不在其他函数中
【发布时间】:2017-02-24 18:50:19
【问题描述】:

我正在尝试为我的智能镜子构建一个刮板。我已经构建了一个 python 脚本来获取天气、污染水平和新闻,而我没有得到的东西就是为什么我在抓取数据而不是天气和污染水平等其他函数时在这个函数块中得到一个错误。这是我遇到错误的代码:

def news(self):
        reddit1 = "https://www.reddit.com/r/LifeProTips/hot.json"
        reddit2 = "https://reddit.com/r/technology/hot.json"
        reddit3 = "https://reddit.com/r/Futurology/hot.json"
        reddit4 = "https://reddit.com/r/Showerthoughts/hot.json"
        lpt = requests.get(reddit1)
        tech = requests.get(reddit2)
        futu = requests.get(reddit3)
        show = requests.get(reddit4)
        lptd = json.loads(lpt.text)
        techd = json.loads(tech.text)
        futud = json.loads(futu.text)
        showd = json.loads(show.text)
        lpttitle = lptd['data']['children'][0]['data']['title']
        print(lpttitle)
        lptcontent = lptd['data']['children'][0]['data']['selftext']
        print(lptcontent)
        techttitle = techd['data']['children'][0]['data']['title']
        print(techtitle)
        techcontent = techd['data']['children'][0]['data']['selftext']
        print(techcontent)
        fututitle = futud['data']['children'][2]['data']['title']
        print(fututitle)
        futucontent = futud['data']['children'][0]['data']['selftext']
        print(futucontent)
        showtitle = showd['data']['children'][0]['data']['title']
        print(showdata)
        showcontent = showd['data']['children'][0]['data']['selftext']
        print(showcontent)
        lptdump = requests.put('firebaseurl',json={"Title": lpttitle, "Content": lptcontent})
        techdump = requests.put('firebaseurl',json={"Title": techtitle, "Content": techcontent})
        futudump = requests.put('firebaseurl',json={"Title": fututitle, "Content": futucontent})
        showdump = requests.put('firebaseurl',json={"Title": showtitle, "Content": showcontent})

但是对于这样的功能:

def pollution(self):
    url = "api"
    url2 = "api"
    pm10 = requests.get(url)
    pm25 = requests.get(url2)
    pm10leveldata = json.loads(pm10.text)
    pm25leveldata = json.loads(pm25.text)
    pm10level = pm10leveldata[0]['data']
    pm25level = pm25leveldata[0]['data']
    print pm10level
    print pm25level
    # pm10 = round(pm10level)
    # pm25 = round(pm25level)
    pdump = requests.put('firebaseurl',json={"Pm10": pm10level, "Pm25": pm25level})

没有错误。为什么我在那个地方得到一个关键错误,但在这样的函数中却没有

【问题讨论】:

  • 错误发生在哪一行?
  • @ColinBasnett 它出现在行上:lpttitle = lptd['data']['children'][0]['data']['title'] 如果我取消注释它会转到其他人
  • 在该行执行之前打印出lptd 并查看密钥是否确实存在。您可能对不正确的数据格式做出了假设。
  • 它打印出 json + 它不再崩溃但它在其他对象上崩溃

标签: python python-2.7 python-requests


【解决方案1】:

在尝试使用之前打印出lptd

{'error': 429, 'message': 'Too Many Requests'}

你激怒了reddit。这可能发生在任何请求上,因此看起来好像错误正在跳跃。

顺便说一句,您可以直接从请求中获取 json:

lptd = requests.get(reddit1).json()

【讨论】:

  • 谢谢伙计。这是问题之一,请在它周围放置一个 time.sleep,以免惹恼它。再加上 idk 如何,但是在打印 lptd 时,其他的都不会崩溃
  • 我也不知道...我前几次运行您的脚本对我有用,然后开始失败。也许它被跟踪了很长时间,并且它的测试运行之间的延迟会产生影响。
猜你喜欢
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多