파이썬으로 크롤링하다보면 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))
출력 결과물(문자열 생략)
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)))
출력 결과물(문자열 생략)
'내가 필요해서 찾아 해본 것들 정리(IT)' 카테고리의 다른 글
파이썬Python 리스트 List 요소 삭제 (0) | 2022.08.24 |
---|---|
파이썬Python 리스트 List 요소 추가 (0) | 2022.08.24 |
동영상으로 움짤(.gif) 만들기 (0) | 2022.08.23 |
파이참 주요 단축키 - 들여쓰기/내어쓰기/주석처리/주석풀기 (0) | 2022.08.23 |
티스토리 블로그에 광고(구글애드센스) 신청하기 (1) | 2022.08.17 |