컴퓨터잡담

파이썬 네이버 증권 주식 현재가 가져오기

by nanumi posted Sep 06, 2021
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄

파이썬 네이버 증권 주식 현재가 가져오기

 

 

 

import requests
import json
def 웹크롤링(codes):
    url = 'https://m.stock.naver.com/api/stock/'+codes+'/basic'
    headers = {
                'Accept': 'application/json, text/javascript, */*; q=0.01',
                'Accept-Encoding': 'gzip, deflate, br',
                'Accept-Language': 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7',
                'Connection': 'keep-alive',
                'access-control-max-age': '86400',
                'Content-Type': 'applicatio/x-www-form-urlencoded; charset=utf-8',
                'Cookie': 'NRTK=ag#all_gr#1_ma#-2_si#0_en#0_sp#0; NNB=MO3FYCZ7G76V6; _ga_1BVHGNLQKG=GS1.1.1614481348.1.1.1614481534.0; ASID=da270011000001781f45a2ad00000064; NID_AUT=P6X6npbh89JB61R04OgkBkG2IJK1P6Rkxtfi6z6wus8V0tqCnum2mH83RRKbEaN4; NID_JKL=PnjcwchxRziN3zVlpnjARIceheALyWZ2sqG6+e0GQYM=; nx_ssl=2; _ga=GA1.2.1929782364.1614481348; _ga_7VKFYR6RV1=GS1.1.1630535466.13.1.1630535531.60; NID_SES=AAABrkhIcPdlcrHA4mTwAeNw+pYyafvsrT3pEBrPEHPlQbV8oSW8tdvsvK+Aka8tVhU7S5WHGKmLgoV9XGCEcPpki/+K3BIrVaWBIst3QnOKD3VX9QopHzE1BizGdVOnAML+6gTsqHmalE4Bc5aZ819L6o0Be7fZT/3ykMX9NwX/p5Vrapxf3EEeNcE2cdALU3A3FXXvJnrokLvpmGplJuw5BOHfX4tuBKUBGMbFH+3KdMT02/DEXrPS7AUVtqsV0JA33t+iuM1prmQKs376mWoMaZgwuLTbfTzXbWP+fr2PX7k+VNqlXUevNFi39TPfAWzYFg7MGxS3Ppa3qkovnOvCx6HgesAhUcxUp60tux2O3WsjQu2RXoZy3q3vPsdjZAu1HeMjWbFiFTrOWJi0h1bjRBAZ9TJi80a7w/t+AovgicS0IR2GCZiyhlsgGSYVEFzM1NTs27p/nJiYUegf4X0kX7Zihjs6KO9XrXMoAdqPqXhBJ6E4nPKqQUJoRPtFtPkQeKJcbWrY/FvHQTaCg1AAyQ73VbYOmUZNvnW3HRrsw1GTzr5tRzSAdwKB2WbBimsj3w==; _naver_usersession_=/ApjYsx4DeVlV2/1ixqYM/KP; page_uid=hghnWdprvOsssP2dZrNssssst5o-514179; MM_NEW=1; NFS=2; BMR=s=1630932935862&r=https%3A%2F%2Fm.stock.naver.com%2FsearchItem.nhn%3FsearchType%3Dinit&r2=https%3A%2F%2Fm.stock.naver.com%2Findex.html; naver_mobile_stock_codeList=128540%7CTSLA.O%2CUSA%7CSBUX.O%2CUSA%7CPCGU.K%2CUSA%7CDCUE.K%2CUSA%7CRIO%2CUSA%7CBHP%2CUSA%7C600177.SS%2CCHN%7C601088.SS%2CCHN%7CBBL%2CUSA%7CWSOb%2CUSA%7CWSO%2CUSA%7CCLVT_pa.K%2CUSA%7CAESC.K%2CUSA%7CABBV.K%2CUSA%7CAPD%2CUSA%7CUSB_pa%2CUSA%7CWFC_pl%2CUSA%7CQTS_pb%2CUSA%7CENR_pa%2CUSA%7CDHR_pb%2CUSA%7CDHR_pa%2CUSA%7CBSX_pa%2CUSA%7CBAC_pl%2CUSA%7CAPTV_pa.K%2CUSA%7CCFXA.K%2CUSA%7CCCI%2CUSA%7CSWT%2CUSA%7CTPL%2CUSA%7CAMT%2CUSA%7C; JSESSIONID=DECE95CB76354300D6777E59F7F12EDC; XSRF-TOKEN=0c1c0122-97ac-4d06-be4c-6b0f173b9a39',
                'Referer': 'https://m.stock.naver.com/index.html',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'
                }
    r = requests.get(url, headers = headers) #Request Method: GET
    data = json.loads(r.text)
    return data

data = 웹크롤링("000020")
print(int(data['closePrice'].replace(",","")))