python urlib AttributeError:' HTTPResponse'对象没有属性' readall'

时间:2017-04-28 04:17:13

标签: python

我正在关注tkinter的教程,其中包含一些urlib,并收到此错误消息。

AttributeError: 'HTTPResponse' object has no attribute 'readall'

这是代码:

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
import urllib
import json
import pandas as pd
import numpy as np
import tkinter as tk
from tkinter import ttk


LARGE_FONT= ("Verdana", 12)
style.use("ggplot")

f = Figure(figsize=(5,3), dpi=100)
a = f.add_subplot(111)


def animate(i):
    dataLink = 'https://btc-e.com/api/3/trades/btc_usd?limit=2000'
    data = urllib.request.urlopen(dataLink)
    data = data.readall().decode("utf-8")
    data = json.loads(data)

这里是完整的追溯

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Programs\Python\Python35\lib\tkinter\__init__.py", line 1550, in __call__
    return self.func(*args)
  File "C:\Programs\Python\Python35\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 280, in resize
    self.show()
  File "C:\Programs\Python\Python35\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 351, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Programs\Python\Python35\lib\site-packages\matplotlib\backends\backend_agg.py", line 464, in draw
    self.figure.draw(self.renderer)
  File "C:\Programs\Python\Python35\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Programs\Python\Python35\lib\site-packages\matplotlib\figure.py", line 1150, in draw
    self.canvas.draw_event(renderer)
  File "C:\Programs\Python\Python35\lib\site-packages\matplotlib\backend_bases.py", line 1815, in draw_event
    self.callbacks.process(s, event)
  File "C:\Programs\Python\Python35\lib\site-packages\matplotlib\cbook.py", line 549, in process
    proxy(*args, **kwargs)
  File "C:\Programs\Python\Python35\lib\site-packages\matplotlib\cbook.py", line 416, in __call__
    return mtd(*args, **kwargs)
  File "C:\Programs\Python\Python35\lib\site-packages\matplotlib\animation.py", line 831, in _start
    self._init_draw()
  File "C:\Programs\Python\Python35\lib\site-packages\matplotlib\animation.py", line 1490, in _init_draw
    self._draw_frame(next(self.new_frame_seq()))
  File "C:\Programs\Python\Python35\lib\site-packages\matplotlib\animation.py", line 1512, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "C:\Programs\Python\Python35\tkinter\TutorialTwo.py", line 28, in animate
    data = data.readall().decode("utf-8")
AttributeError: 'HTTPResponse' object has no attribute 'readall'

1 个答案:

答案 0 :(得分:5)

HttpResponse没有属性readall

>>> dir(data)
['__abstractmethods__', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__',
 '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__',
 '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__',
 '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_abc_cache', '_abc_negative_cache',
 '_abc_negative_cache_version', '_abc_registry', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable',
 '_check_close', '_close_conn', '_get_chunk_left', '_method', '_peek_chunked', '_read1_chunked',
 '_read_and_discard_trailer', '_read_next_chunk_size', '_read_status', '_readall_chunked', '_readinto_chunked',
 '_safe_read', '_safe_readinto', 'begin', 'chunk_left', 'chunked', 'close', 'closed', 'code', 'debuglevel', 'detach',
 'fileno', 'flush', 'fp', 'getcode', 'getheader', 'getheaders', 'geturl', 'headers', 'info', 'isatty', 'isclosed',
 'length', 'msg', 'peek', 'read', 'read1', 'readable', 'readinto', 'readinto1', 'readline', 'readlines', 'reason',
 'seek', 'seekable', 'status', 'tell', 'truncate', 'url', 'version', 'will_close', 'writable', 'write', 'writelines']
>>> type(data)
< class 'http.client.HTTPResponse'>

尝试使用readreadline

data = data.readline().decode("utf-8")