• 카테고리

    질문 & 답변
  • 세부 분야

    금융 · 재테크

  • 해결 여부

    미해결

read_json()관련 질문

23.01.22 13:57 작성 조회수 573

0

웹크롤러 제작 관련 중 오류가 계속해서 나와서 질문하는 바입니다. 샘플코드를 실행해도 다음과 같은 오류가 나서 질문드리는 바입니다. 웹크롤러 120일 전체 데이터를 받아오는 코드에 대해 질문드리는 바입니다.

 df_candle_temp = pd.read_json(webpage.content) 

에 대해 계속해서 오류가 나오는데,

TypeError: Expected file path name or file-like object, got <class 'bytes'> type

라는 말이 나오는데, 이 에러의 표면적인 뜻(?)는 알겠으나, 진짜로 왜 이 에러가 뜨는지, 그리고 어떻게 해결해야 하는지를 잘 모르겠어서 이렇게 질문드리는 바입니다.

아래는 에러 메시지입니다.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[14], line 22
     19 webpage = requests.get(url)
     21 #(3)JSON 형식 데이터 읽어서 임시 데이터프레임에 저장
---> 22 df_candle_temp = pd.read_json(webpage.content) 
     24 #(4) 새로받은 데이터를 기존 데이터프레임과 병합
     25 df_candle = pd.concat([df_candle,df_candle_temp],axis=0) 

File C:\python3111\Lib\site-packages\pandas\util\_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs)
    209     else:
    210         kwargs[new_arg_name] = new_arg_value
--> 211 return func(*args, **kwargs)

File C:\python3111\Lib\site-packages\pandas\util\_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
    325 if len(args) > num_allow_args:
    326     warnings.warn(
    327         msg.format(arguments=_format_argument_list(allow_args)),
    328         FutureWarning,
    329         stacklevel=find_stack_level(),
    330     )
--> 331 return func(*args, **kwargs)

File C:\python3111\Lib\site-packages\pandas\io\json\_json.py:733, in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, encoding_errors, lines, chunksize, compression, nrows, storage_options)
    730 if convert_axes is None and orient != "table":
    731     convert_axes = True
--> 733 json_reader = JsonReader(
    734     path_or_buf,
    735     orient=orient,
    736     typ=typ,
    737     dtype=dtype,
    738     convert_axes=convert_axes,
    739     convert_dates=convert_dates,
    740     keep_default_dates=keep_default_dates,
    741     numpy=numpy,
    742     precise_float=precise_float,
    743     date_unit=date_unit,
    744     encoding=encoding,
    745     lines=lines,
    746     chunksize=chunksize,
    747     compression=compression,
    748     nrows=nrows,
    749     storage_options=storage_options,
    750     encoding_errors=encoding_errors,
    751 )
    753 if chunksize:
    754     return json_reader

File C:\python3111\Lib\site-packages\pandas\io\json\_json.py:818, in JsonReader.__init__(self, filepath_or_buffer, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression, nrows, storage_options, encoding_errors)
    815     if not self.lines:
    816         raise ValueError("nrows can only be passed if lines=True")
--> 818 data = self._get_data_from_filepath(filepath_or_buffer)
    819 self.data = self._preprocess_data(data)

File C:\python3111\Lib\site-packages\pandas\io\json\_json.py:858, in JsonReader._get_data_from_filepath(self, filepath_or_buffer)
    851 filepath_or_buffer = stringify_path(filepath_or_buffer)
    852 if (
    853     not isinstance(filepath_or_buffer, str)
    854     or is_url(filepath_or_buffer)
    855     or is_fsspec_url(filepath_or_buffer)
    856     or file_exists(filepath_or_buffer)
    857 ):
--> 858     self.handles = get_handle(
    859         filepath_or_buffer,
    860         "r",
    861         encoding=self.encoding,
    862         compression=self.compression,
    863         storage_options=self.storage_options,
    864         errors=self.encoding_errors,
    865     )
    866     filepath_or_buffer = self.handles.handle
    867 elif (
    868     isinstance(filepath_or_buffer, str)
    869     and filepath_or_buffer.lower().endswith(
   (...)
    872     and not file_exists(filepath_or_buffer)
    873 ):

File C:\python3111\Lib\site-packages\pandas\io\common.py:900, in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
    895     is_wrapped = not (
    896         isinstance(ioargs.filepath_or_buffer, str) or ioargs.should_close
    897     )
    899 if "r" in ioargs.mode and not hasattr(handle, "read"):
--> 900     raise TypeError(
    901         "Expected file path name or file-like object, "
    902         f"got {type(ioargs.filepath_or_buffer)} type"
    903     )
    905 handles.reverse()  # close the most recently added buffer first
    906 if ioargs.should_close:

TypeError: Expected file path name or file-like object, got <class 'bytes'> type

답변 1

답변을 작성해보세요.

0

안녕하세요 이도영님.

강의에 있는 프로그램을 다시 테스트해 봤는데, 정상적으로 실행되는 것을 확인했습니다.

강의와 다른 패키지를 사용하시는 것이 문제의 원인인 것 같습니다.

먼저 request와 pandas 패키지를 동일하게 설정해보시기 바랍니다.

requests 2.28.0

pandas 1.2.5

제 PC에 설치된 패지키 버전을 첨부합니다.

위와 같이 조치했는데도 계속 문제가 발생하시면 다시 문의 글 남겨주세요.

감사합니다.

C:\Users>pip list
WARNING: Ignoring invalid distribution -rotobuf (c:\python377\lib\site-packages)
Package               Version
--------------------- -----------
absl-py               1.2.0
aiodns                3.0.0
aiohttp               3.8.3
aiosignal             1.3.1
ale-py                0.7.5
APScheduler           3.9.1
argon2-cffi           21.3.0
argon2-cffi-bindings  21.2.0
astor                 0.8.1
async-timeout         4.0.2
asynctest             0.13.0
attrs                 21.4.0
backcall              0.2.0
backports.zoneinfo    0.2.1
bayesian-optimization 1.2.0
BayesianOptimization  0.0.0
beautifulsoup4        4.11.1
bleach                5.0.0
bs4                   0.0.1
cctx                  1.0.0
ccxt                  2.2.36
certifi               2022.5.18.1
cffi                  1.15.0
charset-normalizer    2.0.12
cloudpickle           2.1.0
colorama              0.4.4
cryptography          38.0.3
cycler                0.11.0
debugpy               1.6.0
decorator             5.1.1
defusedxml            0.7.1
entrypoints           0.4
fastjsonschema        2.15.3
fonttools             4.33.3
frozenlist            1.3.3
gast                  0.2.2
google-pasta          0.2.0
grpcio                1.47.0
gym                   0.21.0
gym-notices           0.0.7
h5py                  3.7.0
html5lib              1.1
idna                  3.3
imbalanced-learn      0.9.0
importlib-metadata    4.11.4
importlib-resources   5.7.1
ipykernel             6.13.1
ipython               7.34.0
ipython-genutils      0.2.0
ipywidgets            7.7.0
jedi                  0.18.1
Jinja2                3.1.2
joblib                1.1.0
jsonschema            4.6.0
jupyter               1.0.0
jupyter-client        7.3.4
jupyter-console       6.4.3
jupyter-core          4.10.0
jupyterlab-pygments   0.2.2
jupyterlab-widgets    1.1.0
Keras-Applications    1.0.8
Keras-Preprocessing   1.1.2
kiwisolver            1.4.3
lightgbm              3.3.2
lxml                  4.9.0
Markdown              3.4.1
MarkupSafe            2.1.1
matplotlib            3.5.2
matplotlib-inline     0.1.3
mistune               0.8.4
multidict             6.0.2
nbclient              0.6.4
nbconvert             6.5.0
nbformat              5.4.0
nest-asyncio          1.5.5
notebook              6.4.12
numpy                 1.18.5
opencv-python         4.6.0.66
opt-einsum            3.3.0
packaging             21.3
pandas                1.2.5
pandocfilters         1.5.0
parso                 0.8.3
pickleshare           0.7.5
Pillow                9.1.1
pip                   22.2.2
prometheus-client     0.14.1
prompt-toolkit        3.0.29
protobuf              3.20.0
psutil                5.9.1
pycares               4.2.2
pycparser             2.21
pygame                2.1.0
Pygments              2.12.0
PyJWT                 2.4.0
pyparsing             3.0.9
pyrsistent            0.18.1
python-dateutil       2.8.2
pytz                  2022.1
pytz-deprecation-shim 0.1.0.post0
pyupbit               0.2.33
pywin32               304
pywinpty              2.0.5
pyzmq                 23.1.0
qtconsole             5.3.1
QtPy                  2.1.0
requests              2.28.0
scikit-learn          1.0.2
scipy                 1.7.3
Send2Trash            1.8.0
setuptools            65.6.3
six                   1.16.0
sklearn               0.0
soupsieve             2.3.2.post1
stable-baselines      2.10.2
stable-baselines3     1.6.0
ta                    0.10.2
tensorboard           1.15.0
tensorflow            1.15.0
tensorflow-estimator  1.15.1
termcolor             1.1.0
terminado             0.15.0
threadpoolctl         3.1.0
tinycss2              1.1.1
torch                 1.12.0
tornado               6.1
traitlets             5.2.2.post1
typing_extensions     4.2.0
tzdata                2022.1
tzlocal               2.1
urllib3               1.26.9
wcwidth               0.2.5
webencodings          0.5.1
websocket-client      1.3.3
websockets            10.3
Werkzeug              2.2.1
wheel                 0.37.1
widgetsnbextension    3.6.0
wrapt                 1.14.1
yarl                  1.8.1
zipp                  3.8.0