Developer/Python
한글명을 영문명으로 변환하는 소스 (feat. 네이버 API)
데브포유
2023. 5. 24. 10:28
반응형

import urllib
import time
from urllib import parse
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
naver_url = 'https://dict.naver.com/name-to-roman/translation/?query='
in_file = open("d:\\rep_nm_kor.txt", "r")
out_file = open("d:\\rep_nm_eng.txt", "a")
def ko_to_eng(name):
name_url = naver_url + urllib.parse.quote(name)
req = Request(name_url)
res = urlopen(req)
html = res.read().decode('utf-8')
bs = BeautifulSoup(html, 'html.parser')
name_tags = bs.select('#container > div > table > tbody > tr > td > a')
names = [name_tag.text for name_tag in name_tags]
if len(names) == 0:
return ""
return names[0]
if __name__ == '__main__':
i = 100
for line in in_file:
line = line.rstrip("\n")
out_file.write(line + "," + ko_to_eng(line) + "\n")
i = i - 1
if i == 0:
time.sleep(2)
i = 100
print(line)
in_file.close()
out_file.close()
<< 변환대상 >>
d:\\rep_nm_kor.txt
-----------------------------------
홍길동
이순신
<< 변환결과 >>
d:\\rep_nm_eng.txt
-----------------------------------
홍길동 Hong Gildong
이순신 Lee Soonsin
반응형