๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Development/JSON

JSON

by DevIseo 2022. 3. 6.

์›น ์Šคํฌ๋ž˜ํ•‘

  • ์š”์ฒญ๊ณผ ์‘๋‹ต*****
  • ์š”์ฒญ(์ •๋ณด๋ฅผ ์›ํ•˜๋Š” ์‚ฌ๋žŒ:ํด๋ผ์ด์–ธํŠธ) ——→์ฃผ์†ŒURL ์‘๋‹ต(์ •๋ณด๋ฅผ ์ฃผ๋Š” ์‚ฌ๋žŒ:์„œ๋ฒ„)
  • ๋ฌธ์„œ(HTML, XML,JSON๋“ฑ)<———
  • HTTP →ํ”„๋กœํ† ์ฝœ, ๊ทœ์•…

์š”์ฒญ๊ณผ ์‘๋‹ต*****

  • ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด ์ฃผ์†Œ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ , ์‘๋‹ต ๊ฒฐ๊ณผ๋ฅผ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์›น ํ™”๋ฉด์œผ๋กœ ๋žœ๋”๋ง ํ•œ๋‹ค.
  • ํŒŒ์ด์ฌ์„ ํ†ตํ•ด ์ฃผ์†Œ๋กœ ์š”์ฒญ๋ณด๋‚ด๊ณ , ์‘๋‹ต ๊ฒฐ๊ณผ๋ฅผ ํŒŒ์ด์ฌ์œผ๋กœ ์กฐ์ž‘ํ•œ๋‹ค.

requests

  • request: HTTP for Humans
  • requests๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ํ•ด์ฃผ๋Š” python library
# ์›น ์‚ฌ์ดํŠธ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๊ณ  ์‹ถ๋‹ค.
import requests
from bs4 import BeautifulSoup
# 1. ์ฃผ์†Œ
URL = '<https://finance.naver.com/sise/>'

# 2. ์š”์ฒญ
# <https://docs.python-requests.org/en/latest/user/quickstart/#make-a-request>
# response (200) : ์„ฑ๊ณต์ ์œผ๋กœ ๊ฐ€์ ธ์™”๋‹ค! 404 -์šฐ๋ฆฌ๊ฐ€ ์ž˜๋ชป / 500 - ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž˜๋ชป

response = requests.get(URL)
print(response) #txt ๋ถ™์ผ๋•Œ์™€๋Š” ๋‹ค๋ฆ„! ๋ญ์˜€์ง€??

response = requests.get(URL).text
print(type(response)) # type :string

# 2-1. BeautifulSoup (text -> ๋‹ค๋ฅธ ๊ฐ์ฒด)
# #KOSPI_now
# Beautiful Soup is a Python library for pulling data out of HTML and
# H
data = BeautifulSoup(response, 'html.parser')
# print(type(data), type(response)) # type์ด bs4.BeautifulSoup str๋กœ ๋‹ค๋ฆ„
# 2.2 ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค!
kospi = data.select_one('#KOSPI_now')
print(kospi.text)
  • ์ •๋ณด ์Šคํฌ๋žฉ 1๋‹จ๊ณ„ - ์š”์ฒญ
    • ์ •๋ณด๊ฐ€ ์žˆ๋Š” ์‚ฌ์ดํŠธ URL์„ ํ™•์ธํ•œ๋‹ค.
    • url = ‘https://naver.com
    • URL๋กœ ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.
      • import requests
      • response = requests.get(url).text

 

  • BeautifulSoup
    • html file์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” library
  • [API] Application Programming Intercface
    • ์ปดํ“จํ„ฐ๋‚˜ ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋žจ ์‚ฌ์ด์˜ ์—ฐ๊ฒฐ
    • ์ผ์ข…์˜ ์†Œํ”„ํŠธ์›จ์–ด ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฉฐ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์†Œํ”„ํŠธ์›จ์–ด ์„œ๋น„์Šค๋ฅผ ์ œ๊ณต
    • ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ธฐ์ˆ ํ•˜๋Š” ๋ฌธ์„œ๋‚˜ ํ‘œ์ค€์€ API ์‚ฌ์–‘/๋ช…์„ธ(specification)

 

  • API ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
    • ์š”์ฒญํ•˜๋Š” ๋ฐฉ์‹์— ๋Œ€ํ•œ ์ดํ•ด
      • ์ธ์ฆ ๋ฐฉ์‹
      • URL ์ƒ์„ฑ
        • ๊ธฐ๋ณธ ์ฃผ์†Œ
        • ์›ํ•˜๋Š” ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ๊ฒฝ๋กœ
        • ์š”์ฒญ ๋ณ€์ˆ˜(ํ•„์ˆ˜์™€ ์„ ํƒ)
      • ์‘๋‹ต ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์ดํ•ด
        • ์‘๋‹ต ๊ฒฐ๊ณผ ํƒ€์ž…(JSON)
        • ์‘๋‹ต ๊ฒฐ๊ณผ ๊ตฌ์กฐ
  • API ๋ช…์„ธ ์˜ˆ์‹œ(๋„ค์ด๋ฒ„์˜ํ™”๊ฒ€์ƒ‰, ๊ณต๊ณต๋ฐ์ดํ„ฐํฌํ„ธ,์นด์นด์˜คํ†ก)
  • API ๊ฐ„๋‹จ ํ™œ์šฉ ์‹ค์Šต
import requests

# 1. URL
# ์š”์ฒญ ๋ณ€์ˆ˜ : ?name=michael
r = requests.get('<https://api.agify.io?name=michael>'
URL = '<https://api.agify.io>'
params = { 'name': 'michael' }

# 2. ์š”์ฒญ
response = requests.get(URL).json()
print(response)
print(response.get('age'))

ํŒŒ์ผ๋ฐ์ดํ„ฐ →๊ณผ๊ฑฐ๋ฐ์ดํ„ฐ, ์˜คํ”ˆapi →์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ

# 0. import
import requests
# 1. URL ๋ฐ ์š”์ฒญ๋ณ€์ˆ˜ ์„ค์ •
# https://developers.themoviedb.org/3/movies/now_playing

#http๋กœ ์š”์ฒญ๋ณด๋‚ผ๊ฑฐ์•ผ, ์ฃผ์†Œ๊ฐ€ ์žˆ๊ณ  ๊ทธ ์ฃผ์†Œ์— ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ฐ’์ด ์žˆ์–ด
#params๋ฅผ url๋กœ ๋งŒ๋“œ๋Š” ๋ฒ•์€ ์•„๋žซ์ค„๊ณผ ๊ฐ™์Œ
# https://api.themoviedb.org/3/movie/now_playing?api_key=8854669b886a6c07c12ea947bcc2311d&region=KR&language=ko

BASE_URL = 'https://api.themoviedb.org/3'
path = '/movie/now_playing'
params = {'api_key' : '8854669b886a6c07c12ea947bcc2311d', 'region' : 'KR', 'language' : 'ko'}

# 2. ์š”์ฒญ ๋ณด๋‚ธ ๊ฒฐ๊ณผ ์ €์žฅ
response = requests.get(BASE_URL+path, params=params).json()
print(response.status_code, response.url)
data = response.json()
# 3. ์กฐ์ž‘...
print(response)

 

 

 

'Development > JSON' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

JSON  (0) 2022.03.06
JSON  (0) 2022.03.06

๋Œ“๊ธ€