인프런 파이썬 입문 수업을 듣고 중요한 내용을 정리했습니다.
개인 공부 후 자료를 남기기 위한 목적이므로 내용 상에 오류가 있을 수 있습니다.
크롤링을 위한 지식(Python)
Open API(Rest API)와 JSON 포멧의 기본개념을 다루고자 한다.
Open API(Rest API) 란?
- API :
Application Programming Interface의 약자로, 특정 프로그램을 만들기 위해 제공되는 모듈(함수 등)을 의미한다.
- Open API :
공개 API라고도 불리우며, 누구나 사용할 수 있도록 공개된 API로 주로 Rest API 기술을 많이 사용된다.
- Rest API :
Representational State Transfer API의 약자로, HTTP 프로토콜을 통해 서버 제공 기능을 사용할 수 있는 함수를
의미한다. [일반적으로 XML, JSON의 형태로 응답을 전달함 (원하는 데이터의 추출이 수월)]
JSON 이란?
JavaScript Object Notation의 약자로, 웹 환경에서 서버와 클라이언트 사이에 데이터를 주고 받을 때 주로 사용된다.
- JSON 포멧의 예시
{ "id":"01", "language": "Java", "edition": "third", "author": "Herbert Schildt" }
import json
data = '''
{
"lastBuildDate": "Mon, 01 Nov 2021 13:18:23 +0900",
"total": 1880591,
"start": 1,
"display": 10,
"items": [
{
"title": "이스트라 AN430UHD <b>안드로이드</b> 더 스마트 AI",
"link": "https://search.shopping.naver.com/gate.nhn?id=25154449522",
"image": "https://shopping-phinf.pstatic.net/main_2515444/25154449522.20210831145636.jpg",
"lprice": "327700",
"hprice": "",
"mallName": "네이버",
"productId": "25154449522",
"productType": "1",
"brand": "이스트라",
"maker": "이스트라",
"category1": "디지털/가전",
"category2": "영상가전",
"category3": "TV",
"category4": "LEDTV"
},
{
"title": "라익미 스마트 UV650 크롬캐스트 <b>안드로이드</b> IPS",
"link": "https://search.shopping.naver.com/gate.nhn?id=27776369522",
"image": "https://shopping-phinf.pstatic.net/main_2777636/27776369522.20211029155349.jpg",
"lprice": "598950",
"hprice": "",
"mallName": "네이버",
"productId": "27776369522",
"productType": "1",
"brand": "라익미",
"maker": "라익미",
"category1": "디지털/가전",
"category2": "영상가전",
"category3": "TV",
"category4": "LEDTV"
},
{
"title": "이스트라 AN550UHD <b>안드로이드</b> 더 스마트 AI",
"link": "https://search.shopping.naver.com/gate.nhn?id=23737174490",
"image": "https://shopping-phinf.pstatic.net/main_2373717/23737174490.20210831145710.jpg",
"lprice": "472090",
"hprice": "",
"mallName": "네이버",
"productId": "23737174490",
"productType": "1",
"brand": "이스트라",
"maker": "이스트라",
"category1": "디지털/가전",
"category2": "영상가전",
"category3": "TV",
"category4": "LEDTV"
},
{
"title": "이스트라 AN750UHD <b>안드로이드</b> 더 스마트 AI",
"link": "https://search.shopping.naver.com/gate.nhn?id=22991603490",
"image": "https://shopping-phinf.pstatic.net/main_2299160/22991603490.20210831150202.jpg",
"lprice": "999700",
"hprice": "",
"mallName": "네이버",
"productId": "22991603490",
"productType": "1",
"brand": "이스트라",
"maker": "이스트라",
"category1": "디지털/가전",
"category2": "영상가전",
"category3": "TV",
"category4": "LEDTV"
},
{
"title": "라익미 스마트 UV430 크롬캐스트 <b>안드로이드</b>",
"link": "https://search.shopping.naver.com/gate.nhn?id=28886372586",
"image": "https://shopping-phinf.pstatic.net/main_2888637/28886372586.20211029155443.jpg",
"lprice": "348980",
"hprice": "",
"mallName": "네이버",
"productId": "28886372586",
"productType": "1",
"brand": "라익미",
"maker": "라익미",
"category1": "디지털/가전",
"category2": "영상가전",
"category3": "TV",
"category4": "LEDTV"
},
{
"title": "라익미 스마트 QV750 크롬캐스트 <b>안드로이드</b> QLED",
"link": "https://search.shopping.naver.com/gate.nhn?id=28161128522",
"image": "https://shopping-phinf.pstatic.net/main_2816112/28161128522.20210806154035.jpg",
"lprice": "1049000",
"hprice": "",
"mallName": "네이버",
"productId": "28161128522",
"productType": "1",
"brand": "라익미",
"maker": "라익미",
"category1": "디지털/가전",
"category2": "영상가전",
"category3": "TV",
"category4": "QLEDTV"
},
{
"title": "프리즘코리아 A43i google <b>android</b> TV BT50",
"link": "https://search.shopping.naver.com/gate.nhn?id=23401556494",
"image": "https://shopping-phinf.pstatic.net/main_2340155/23401556494.20210129142758.jpg",
"lprice": "349000",
"hprice": "",
"mallName": "네이버",
"productId": "23401556494",
"productType": "1",
"brand": "프리즘코리아",
"maker": "프리즘코리아",
"category1": "디지털/가전",
"category2": "영상가전",
"category3": "TV",
"category4": "LEDTV"
},
{
"title": "라익미 스마트 QV650 크롬캐스트 <b>안드로이드</b> QLED",
"link": "https://search.shopping.naver.com/gate.nhn?id=28695494556",
"image": "https://shopping-phinf.pstatic.net/main_2869549/28695494556.20210907190947.jpg",
"lprice": "698280",
"hprice": "",
"mallName": "네이버",
"productId": "28695494556",
"productType": "1",
"brand": "라익미",
"maker": "라익미",
"category1": "디지털/가전",
"category2": "영상가전",
"category3": "TV",
"category4": "QLEDTV"
},
{
"title": "더함 UA431UHD",
"link": "https://search.shopping.naver.com/gate.nhn?id=28788691554",
"image": "https://shopping-phinf.pstatic.net/main_2878869/28788691554.20210910114604.jpg",
"lprice": "359000",
"hprice": "",
"mallName": "네이버",
"productId": "28788691554",
"productType": "1",
"brand": "더함",
"maker": "더함",
"category1": "디지털/가전",
"category2": "영상가전",
"category3": "TV",
"category4": "LEDTV"
},
{
"title": "프리즘코리아 A55i google <b>android</b> TV BT50",
"link": "https://search.shopping.naver.com/gate.nhn?id=23401556495",
"image": "https://shopping-phinf.pstatic.net/main_2340155/23401556495.20210716144026.jpg",
"lprice": "509000",
"hprice": "",
"mallName": "네이버",
"productId": "23401556495",
"productType": "1",
"brand": "프리즘코리아",
"maker": "프리즘코리아",
"category1": "디지털/가전",
"category2": "영상가전",
"category3": "TV",
"category4": "LEDTV"
}
]
}
'''
json_data = json.loads(data)
print(json_data['lastBuildDate'])
print(json_data['items'][0]['title'])
print(json_data['items'][0]['link'])
```
결과 :
Mon, 01 Nov 2021 13:18:23 +0900
이스트라 AN430UHD <b>안드로이드</b> 더 스마트 AI
https://search.shopping.naver.com/gate.nhn?id=25154449522
```
위의 예시코드에서 변수 data에 할당한 json 데이터는 네이버 Open API를 사용해서 받은 것이다.
(네이버 쇼핑에서 android 키워드로 검색한 상품 리스트 결과)
json_data = json.loads(data)의 코드는 json 라이브러리의 loads() 함수를 사용하여 json 형태로 받은 데이터를
파싱(분석)한 후에 변수 json_data에 넣어주는 것을 의미한다.
한편, 변수 json_data에 데이터를 넣어주면 이후에는 딕셔너리 문법을 사용해서 원하는 데이터만을 손쉽게 가져올 수
있다. (ex. json_data['items'][0]['title'])
'Python' 카테고리의 다른 글
Python - 정부 공공 데이터 Open API로 크롤링 하기 (0) | 2021.11.03 |
---|---|
Python - 네이버 검색 Open API를 활용한 크롤링 (1) | 2021.11.01 |
Python - 엑셀파일과 크롤링 (0) | 2021.11.01 |
Python - 크롤링 기법 (0) | 2021.10.31 |
Python - 크롤링 라이브러리 비교(requests vs urllib) (0) | 2021.10.31 |
댓글