'파이썬으로 네이버 주식 현재가 가져오기'에 해당되는 글 1건

728x90

네이버 주식 정보 페이지에서 내가 원하는 정보만 가져오기 위해서 먼저 크롬브라우저에서 F12키를 눌러서 html 소스코드 보기를 한다.

 

아래 번호 순서대로 해보자. 먼저 1번을 눌러주면 마우스를 가져가는 곳에 해당되는 html 코드를 반환해준다.

2번 위치에 마우스를 가져가면 해당되는 태그 정보를 알려준다.

 

이제 파이썬 소스 코드를 작성하면서 중간 중간에 찍어보면서 원하는 결과를 추출하면 된다.

# 네이버 주식정보 가져오기
# BeautifulSoup은 HTML 과 XML 파일로부터 데이터를 수집하는 라이브러리
# pip install bs4
# pip install requests
# pip install fake-useragent
 
from bs4 import BeautifulSoup
import requests
from fake_useragent import UserAgent
from datetime import datetime
import time
 
def getCode(company_code):
    # company_code : 기업코드
    url ="https://finance.naver.com/item/main.nhn?code=" + company_code
    ua = UserAgent()
    # 헤더 정보 (http 해킷 헤더에 브라우저 정보가 존재하는지 확인할 경우)
    headers = { 'User-agent': ua.ie }
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.content, "html.parser")
    return soup
 
def getPrice(company_code):
    soup = getCode(company_code)
    # no_today = soup.find("p", {"class": "no_today"})
    no_today = soup.select_one('p.no_today')
    # print(no_today) # 출력을 한 다음에 더 세부적인 정보를 파싱처리한다.
    # blind = no_today.find("span", {"class" : "blind"})
    blind = no_today.select_one('span.blind')
    return blind.text
 
# 증시 기업코드
company_codes = ["030200""005930""068270""035720"]
 
if __name__ == '__main__':
    now = datetime.now()
    print("-" * 60)
    print(now)
    print("-" * 60)
 
    for elm in company_codes:
        nowPrice = getPrice(elm)
        print(nowPrice)
    print("-" * 60)
 

 

결과 출력 화면

 

샘플 소스코드

crawling_10.py
0.00MB

블로그 이미지

Link2Me

,