VScode에 naver-book-api.py 생성 후 실습 진행
MySQL에서 bookdb 를 만들고, bookdb에 대한 사용자 권한 부여
책 정보에 대한 table 만들기
import urllib.request
import json # 내장 모듈
import mysql.connector
# API 호출을 위한 client_id, client_secret 변수 설정
client_id = 'ECmEQr0_2x7miL8FQaey'
client_secret = 'eubz_ZNeLw'
searchText = urllib.parse.quote('소나기')
url = 'https://openapi.naver.com/v1/search/book.json?query=' + searchText + '&display=100'
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)
response_body = response.read()
response_body = json.loads(response_body)
# print(response_body)
book_list = response_body['items']
connection = mysql.connector.connect(
host="localhost",
user="ohgiraffers",
password="ohgiraffers",
database="bookdb",
)
cursor = connection.cursor()
sql = "INSERT INTO naver_book (book_title, book_image, author, publisher, isbn, book_description, pub_date) VALUES (%s, %s, %s, %s, %s, %s, %s)"
for book_info in book_list:
values = (book_info['title'], book_info['image'], book_info['author'],
book_info['publisher'], str(book_info['isbn']),
book_info['description'], book_info['pubdate'])
cursor.execute(sql, values)
connection.commit()
cursor.close()
connection.close()
'Web Crawling > 실습' 카테고리의 다른 글
| 네이버 API를 활용한 뉴스 검색 (2) | 2025.02.23 |
|---|