본문 바로가기

Language/python

[ Python library ] 주식 현재가 추출 함수(naver증권 webcrolling)

반응형

import requests
from bs4 import BeautifulSoup
import urllib3

shcode ='004960' #한신공영

# 주식의 현재가 추출 함수
def get_currentprice(shcode):
    urllib3.disable_warnings()
    URL = "https://finance.naver.com/item/main.nhn?code=" + shcode
    response_data = requests.get(URL, verify=False, headers={
        'referer': "https://navercomp.wisereport.co.kr/v2/company/c1030001.aspx?cmp_cd=005930&cn="})

    html = response_data.text
    soup = BeautifulSoup(html, 'html.parser')

    currentprice = int(soup.select('.trade_compare tr td')[0].contents[0].replace(',', ''))
    return currentprice

print(get_currentprice('004960'))

반응형