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()
Hello, I am a student who is thinking about making a search mind map application using Naver Openapi.
It is possible to change url by changing sNode in the code above, which is #
I want to print out the search results of news, blog, and cafe at the same time, is there any way?
If it's OK as long as all three are printed out, the story is very simple.
def main() :
jsonResult = []
for sNode in ('news', 'blog', 'cafearticle') :
# All the remaining code parts referencing the sNode in the main() are moved below
And maybe this is far from what you think.
Exactly which one you're calling "simultaneous output" would be nice to have that exact technical specification (ex. print out any of the three in real time if you have a recent one)
568 Who developed the "avformat-59.dll" that comes with FFmpeg?
599 GDB gets version error when attempting to debug with the Presense SDK (IDE)
607 Uncaught (inpromise) Error on Electron: An object could not be cloned
886 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.