๋๋ ์ด๊ฒ์ Python 3.6.1 ๋ฐ 3.6.2์ ํจ๊ป v. 2.18.4 ๋ฐ 2.14.2 ์์ฒญ์ผ๋ก ์ฌํํ์ต๋๋ค. ์์คํ ์ ์ค์น๋ Python/์์ฒญ(python.org์์ ๋ค์ด๋ก๋ํ Python 3.6.1์ด ์ค์น๋ macOS Sierra), conda 4.3.25์ ์ผ๋ถ๋ก ์ค์น๋ Python/์์ฒญ, conda-forge์์ ์ค์น๋ ์ต์ Python ๋ฐ ์์ฒญ์ ์ฌ์ฉํ์ต๋๋ค.
๋ชจ๋ ๊ณณ์์ ๋์ผํ ๊ฒฐ๊ณผ.
http://docs.python-requests.org/en/master/์ ํ์๋ ๋๋ก JSON ๊ฐ ์ฌ์ ์ ๋ณผ ๊ฒ์ผ๋ก ์์๋ฉ๋๋ค.
Python ์ธํฐํ๋ฆฌํฐ๊ฐ ์ถฉ๋ํฉ๋๋ค.
Python 3.6.2 | packaged by conda-forge | (default, Jul 23 2017, 23:01:38)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> r = requests.get('https://pypi.python.org/pypi')
>>> r.status_code
200
>>> r.encoding
'utf-8'
>>> r.json()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/psimpson/miniconda3/envs/test/lib/python3.6/site-packages/requests/models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "/Users/psimpson/miniconda3/envs/test/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/Users/psimpson/miniconda3/envs/test/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/psimpson/miniconda3/envs/test/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
$ python -m requests.help
{
"chardet": {
"version": "3.0.4"
},
"cryptography": {
"version": "2.0.3"
},
"idna": {
"version": ""
},
"implementation": {
"name": "CPython",
"version": "3.6.2"
},
"platform": {
"release": "16.7.0",
"system": "Darwin"
},
"pyOpenSSL": {
"openssl_version": "100020cf",
"version": "17.2.0"
},
"requests": {
"version": "2.18.4"
},
"system_ssl": {
"version": "100020cf"
},
"urllib3": {
"version": "1.22"
},
"using_pyopenssl": true
}
@justlurking , ์ด๊ฒ์ ์ค์ ๋ก ์์๋๋ก ์๋ํฉ๋๋ค. .json() ๋ฉ์๋๋ json ํ์์ ์๋ต ์ฝํ ์ธ ์์๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค. ์์ฒญํ์ ์นํ์ด์ง๊ฐ HTML ๋ฌธ์๋ฅผ ๋ฐํํ๋ฏ๋ก ์๋ํ์ง ์์ต๋๋ค.
Accept ํค๋๋ฅผ ์์ ํ๊ณ ์ ํจํ json ๋ฐ์ดํฐ ๊ฒ์์ ์๋ ํ๊ฑฐ๋ json ๋ฐ์ดํฐ๋ฅผ ์ฆ์ ์ฌ์ฉํ ์ ์๋ ๊ฒฝ์ฐ๋ฅผ ์ฒ๋ฆฌํ๋ ์ฝ๋๋ฅผ ์ถ๊ฐํด์ผ ํฉ๋๋ค. ์ผ์ด ์์๋๋ก ์๋ํ๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ ๋ซ์ต๋๋ค.
๋งค์ฐ ๋น ๋ฅด๊ณ ์ ์ตํ ๋ต๋ณ์ ๊ฐ์ฌ๋๋ฆฌ๋ฉฐ ์๊ฐ์ ๋ญ๋นํ ์ ์ ๋ํด ์ฌ๊ณผ๋๋ฆฝ๋๋ค. ๋ด๊ฐ ํ ์คํธํ ์ฌ๋ฌ ๋ฒ์ ๊ณผ GitHub์ ๋ํ ํญ์์ ๋ถ์กฑ์ ๊ฐ์ํ ๋ ์ด์ ๋น์ทํ ์ ์๋ค๋ ๊ฒ์ ๊นจ๋ฌ์์ด์ผ ํ์ต๋๋ค. ๐คฆ
๊ฐ์ฅ ์ข์ ๋ฐฉ๋ฒ์ json.JSONDecodeError๋ฅผ ๊ฐ์ ธ์ค๊ณ ์ด์ ๊ฐ์ ์์ ์ ์ํํ๋ ๊ฒ์ ๋๋ค.
try:
r.json()
except JSONDecodeError as e:
<handle exception>
๋ฐ๋ผ์ ์ด๊ฒ์ ์ค์ ๋ก ValueError์ ํ์ ํด๋์ค์ฌ์ผ ํ๋ฏ๋ก ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์ํ์ง ์๋ ๊ฒฝ์ฐ ์ด๋ฅผ ์ก์ ์ ์์ ๊ฒ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค. ์ํ๋ ํน์ ์์ค์ ๋ฐ๋ผ ๋ค๋ฆ ๋๋ค.
๊ทธ ์ผ๋ฐ์ ์ธ ์์ด๋์ด๋ ๋ด๊ฐ ์ ์ํ๋ ์ ๊ทผ ๋ฐฉ์์ ๋๋ค. ๋์์ด ๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค!
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
@justlurking , ์ด๊ฒ์ ์ค์ ๋ก ์์๋๋ก ์๋ํฉ๋๋ค. .json() ๋ฉ์๋๋ json ํ์์ ์๋ต ์ฝํ ์ธ ์์๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค. ์์ฒญํ์ ์นํ์ด์ง๊ฐ HTML ๋ฌธ์๋ฅผ ๋ฐํํ๋ฏ๋ก ์๋ํ์ง ์์ต๋๋ค.
Accept ํค๋๋ฅผ ์์ ํ๊ณ ์ ํจํ json ๋ฐ์ดํฐ ๊ฒ์์ ์๋ ํ๊ฑฐ๋ json ๋ฐ์ดํฐ๋ฅผ ์ฆ์ ์ฌ์ฉํ ์ ์๋ ๊ฒฝ์ฐ๋ฅผ ์ฒ๋ฆฌํ๋ ์ฝ๋๋ฅผ ์ถ๊ฐํด์ผ ํฉ๋๋ค. ์ผ์ด ์์๋๋ก ์๋ํ๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ ๋ซ์ต๋๋ค.