편집 기록

편집 기록
  • 프로필 엽토군님의 편집
    날짜2021.03.21

    오픈 api / url 여러개 사용 질문드립니다.


    import os
    import sys
    import urllib.request
    import datetime
    import time
    import json
    #from config import *
    
    
    # [CODE 1]
    def get_request_url(url):
        req = urllib.request.Request(url)
        req.add_header("X-Naver-Client-Id", "clien-id")
        req.add_header("X-Naver-Client-Secret", "password)
        try:
            response = urllib.request.urlopen(req)
            if response.getcode() == 200:
                print("[%s] Url Request Success" % datetime.datetime.now())
                return response.read().decode('utf-8')
        except Exception as e:
            print(e)
            print("[%s] Error for URL : %s" % (datetime.datetime.now(), url))
            return None
    
    
    # [CODE 2]
    def getNaverSearchResult(sNode, search_text, page_start, display):
        base = "https://openapi.naver.com/v1/search"
        node = "/%s.json" % sNode
        parameters = "?query=%s&start=%s&display=%s" % (urllib.parse.quote(search_text), page_start, display)
        url = base + node + parameters
    
        retData = get_request_url(url)
    
        if (retData == None):
            return None
        else:
            return json.loads(retData)
    
    
    # [CODE 3]
    def getPostData(post, jsonResult):
        title = post['title']
        description = post['description']
        org_link = post['originallink']
        link = post['link']
    
        pDate = datetime.datetime.strptime(post['pubDate'], '%a, %d %b %Y %H:%M:%S +0900')
        pDate = pDate.strftime('%Y-%m-%d %H:%M:%S')
    
        jsonResult.append({'title': title, 'description': description,
                           'org_link': org_link, 'link': org_link,
                           'pDate': pDate})
        return
    
    
    def main():
        jsonResult = []
    
        # 'news', 'blog', 'cafearticle'
        sNode = 'news'
        search_text = 'keyword'
        display_count = 100
    
        jsonSearch = getNaverSearchResult(sNode, search_text, 1, display_count)
    
        while ((jsonSearch != None) and (jsonSearch['display'] != 0)):
            for post in jsonSearch['items']:
                getPostData(post, jsonResult)
    
            nStart = jsonSearch['start'] + jsonSearch['display']
            jsonSearch = getNaverSearchResult(sNode, search_text, nStart, display_count)
    
        with open('%s_naver_%s.json' % (search_text, sNode), 'w', encoding='utf8') as outfile:
            retJson = json.dumps(jsonResult,
                                 indent=4, sort_keys=True,
                                 ensure_ascii=False)
            outfile.write(retJson)
    
        print('%s_naver_%s.json SAVED' % (search_text, sNode))
    
    
    if __name__ == '__main__':
        main()
    

    안녕하세요 네이버 오픈api를 이용해서 검색 마인드맵 어플을 만들고 싶어서 고민중인 학생입니다.
    위의 코드에서 sNode를 변경하여 url을 바꾸는 것은 가능한대, #으로 되어있는 news, blog, cafearticle의 검색결과를 동시에 출력을 하고 싶은데 혹시 방법이 없을까요?

  • 프로필 알 수 없는 사용자님의 편집
    날짜2021.03.21

    오픈 api / url 여러개 사용 질문드립니다.


    import os import sys import urllib.request import datetime import time import json

    from config import *

    [CODE 1]

    def get_request_url(url): req = urllib.request.Request(url) req.add_header("X-Naver-Client-Id", "clien-id") req.add_header("X-Naver-Client-Secret", "password) try: response = urllib.request.urlopen(req) if response.getcode() == 200: print("[%s] Url Request Success" % datetime.datetime.now()) return response.read().decode('utf-8') except Exception as e: print(e) print("[%s] Error for URL : %s" % (datetime.datetime.now(), url)) return None

    [CODE 2]

    def getNaverSearchResult(sNode, search_text, page_start, display): base = "https://openapi.naver.com/v1/search" node = "/%s.json" % sNode parameters = "?query=%s&start=%s&display=%s" % (urllib.parse.quote(search_text), page_start, display) url = base + node + parameters

    retData = get_request_url(url)
    
    if (retData == None):
        return None
    else:
        return json.loads(retData)
    

    [CODE 3]

    def getPostData(post, jsonResult): title = post['title'] description = post['description'] org_link = post['originallink'] link = post['link']

    pDate = datetime.datetime.strptime(post['pubDate'], '%a, %d %b %Y %H:%M:%S +0900')
    pDate = pDate.strftime('%Y-%m-%d %H:%M:%S')
    
    jsonResult.append({'title': title, 'description': description,
                       'org_link': org_link, 'link': org_link,
                       'pDate': pDate})
    return
    

    def main(): jsonResult = []

    # 'news', 'blog', 'cafearticle'
    sNode = 'news'
    search_text = 'keyword'
    display_count = 100
    
    jsonSearch = getNaverSearchResult(sNode, search_text, 1, display_count)
    
    while ((jsonSearch != None) and (jsonSearch['display'] != 0)):
        for post in jsonSearch['items']:
            getPostData(post, jsonResult)
    
        nStart = jsonSearch['start'] + jsonSearch['display']
        jsonSearch = getNaverSearchResult(sNode, search_text, nStart, display_count)
    
    with open('%s_naver_%s.json' % (search_text, sNode), 'w', encoding='utf8') as outfile:
        retJson = json.dumps(jsonResult,
                             indent=4, sort_keys=True,
                             ensure_ascii=False)
        outfile.write(retJson)
    
    print('%s_naver_%s.json SAVED' % (search_text, sNode))
    

    if name == 'main': main()

    안녕하세요 네이버 오픈api를 이용해서 검색 마인드맵 어플을 만들고 싶어서 고민중인 학생입니다. 위의 코드에서 sNode를 변경하여 url을 바꾸는 것은 가능한대, #으로 되어있는 news, blog, cafearticle의 검색결과를 동시에 출력을 하고 싶은데 혹시 방법이 없을까요?