//네이버

 

 

파이썬으로 크롤링하다보면 json양식으로 되어있는 긴 문자열을 받아올 때가 있다.

{"success":true,"data":{"total_count":2491,"total_pages":125,"list":[{"created_at": .... 

와 같은 양식으로 마치 파이썬 Dict와 비슷해보이지만 Dict와 완벽하게 일치하진 않아서 eval로는 변경되지는 않는다

 

원활한 편집을 위해서 딕셔너리로 변경하고자 하면 json을 통해 매우 간단하게 변경할 수 있다.

 

 

예제

타겟 링크: https://api-manager.upbit.com/api/v1/notices?page=1&per_page=20&thread_name=general 

 

1. 텍스트로 받았을때 

import requests


url = 'https://api-manager.upbit.com/api/v1/notices?page=1&per_page=20&thread_name=general'

res = requests.get(url)

print(res.text)
print(type(res.text))

출력 결과물(문자열 생략)

Type은 문자열

 

2. json으로 변환 후 

import requests
import json


url = 'https://api-manager.upbit.com/api/v1/notices?page=1&per_page=20&thread_name=general'

res = requests.get(url)


print(json.loads(res.text))
print(type(json.loads(res.text)))

출력 결과물(문자열 생략)

 

+ Recent posts