본문 바로가기
카테고리 없음

python package 사용

by 책읽는구리 2020. 2. 10.
반응형

The Python Standard Library
https://docs.python.org/3/library/index.html

PyPI - the Python Package Index
https://pypi.org/

# 패키지 설치
pip3 install requests

# 패키지 import
ipython3
import requests

In [2]: requests.get('http://nasa.gov')
Out[2]: 

In [3]: resp = requests.get('http://nasa.gov')

In [4]: resp.status_code
Out[4]: 200

In [5]: resp.text
Out[5]: '\nIn [6]: meteor_resp = requests.get('https://data.nasa.gov/resource/y77d-th95.json') 

=> json 형태로 제공

 

In [7]: meteor_resp.status_code

Out[7]: 200

 

In [8]: meteor_resp.text

Out[8]: '[{"name":"Aachen","id":"1","nametype":"Valid","recclass":"L5","mass":"21","fall

=> json 데이터

In [8]: meteor_resp.json()

Out[8]: [{'name': 'Tomakovka',   'id': '24019',   'nametype': 'Valid',   'recclass': 'LL6',   'mass': '600',   'fall': 'Fell',   'year': '1905-01-01T00:00:00.000',   'reclat': '47.850000',   'reclong': '34.766670',   'geolocation': {'type': 'Point', 'coordinates': [34.76667, 47.85]}}]

 

In [12]: meteor_data = meteor_resp.json()

In [13]: type(meteor_data)

Out[13]: list   

=> meteor_data 타입이 list 이기 때문에 iterable 함.

 

In [14]: for meteor in meteor_data: print(type(meteor))

<class 'dict'>

<class 'dict'> ...

 

In [15]: meteor_data[0]

Out[15]: {'name': 'Aachen',  'id': '1',  'nametype': 'Valid',  'recclass': 'L5',  'mass': '21',  'fall': 'Fell',  'year': '1880-01-01T00:00:00.000',  'reclat': '50.775000',  'reclong': '6.083330',  'geolocation': {'type': 'Point', 'coordinates': [6.08333, 50.775]}}            ​

반응형

댓글