웹 크롤링 파이썬 10 [naver api 이용하기]

[예제]


# 네이버 검색 API예제는 블로그를 비롯 전문자료까지 호출방법이 동일하므로 blog검색만 대표로 예제를 올렸습니다.# 네이버 검색 Open API 예제 - 블로그 검색import os
import sys
import urllib.request
import requests
from urllib.parse import urlparse

keyword = "강남역"client_id = "발급받아서사용"client_secret = "발급받아서사용"
#encText = urllib.parse.quote("강남역")url = "https://openapi.naver.com/v1/search/blog?query=" + keyword # json 결과result = requests.get(urlparse(url).geturl(),
                      headers = {"X-Naver-Client-Id":client_id,
                      "X-Naver-Client-Secret":client_secret})

json_obj = result.json()
print(json_obj['lastBuildDate'])
print(json_obj['total'])
print(json_obj['start'])
print(json_obj['display'])
#print(result.json())
json_obj2 = {"name":"krt",
             "age":"33",
             "where":"역삼동",
             "phone_number":"010-3584-0000",
             "friends":[{"name":"sian","age":"32"},
                        {"name":"kyuri","age":"24"}]
             }

print(json_obj2['name'])
print(json_obj2['phone_number'])
print(json_obj2['friends'])

friends = json_obj2['friends']

for friend in friends:
    print(friend['age'])
    








'''#client_id = ""#client_secret = ""encText = urllib.parse.quote("강남역")#url = "https://openapi.naver.com/v1/search/blog?query=" + encText # json 결과# url = "https://openapi.naver.com/v1/search/blog.xml?query=" + encText # xml 결과request = urllib.request.Request(url)request.add_header("X-Naver-Client-Id",client_id)request.add_header("X-Naver-Client-Secret",client_secret)response = urllib.request.urlopen(request)rescode = response.getcode()if(rescode==200):    response_body = response.read()    print(response_body.decode('utf-8'))else:    print("Error Code:" + rescode)'''

[결과]


Wed, 06 Mar 2019 22:09:21 +0900
1904463
1
10
krt
010-3584-0000
[{'name': 'sian', 'age': '32'}, {'name': 'kyuri', 'age': '24'}]
32
24
웹 크롤링 파이썬 10 [naver api 이용하기] 웹 크롤링 파이썬 10 [naver api 이용하기] Reviewed by kukanuc on 3월 06, 2019 Rating: 5

댓글 없음:

Powered by Blogger.