카테고리 없음

국민연금 주식 리스트 파이썬 코드2

노트에버 2023. 7. 13. 17:19

네이버 주식 페이지  캡쳐하여  한페이지에 보기 

# code 주식 번호 입력

vscode 진행 

버전 

/usr/bin/chromedriver -v
ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378})

selenium    4.10.0

#실행방법

 wsl2 //

python3.10 app.py   아래와 같이 실행창 뜸

 

크롬에서 127.0.0.1:5000  치면 캡쳐 된 내용이 나옴 

 

import requests
from flask import Flask, render_template_string
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

app = Flask(__name__)


def capture_web_page(url):
    chrome_options = Options()
    chrome_options.add_argument("--headless")  # 브라우저 창을 띄우지 않고 실행
    chrome_options.add_argument("--disable-gpu")  # GPU 가속 사용 비활성화

    # Chrome WebDriver의 경로를 설정해주세요.
    service = Service(executable_path="/usr/local/bin/chromedriver")
    driver = webdriver.Chrome(service=service, options=chrome_options)

    # 페이지 접속
    driver.get(url)

    # 페이지 내용 캡처
    html = driver.page_source

    # WebDriver 종료
    driver.quit()

    return html


def crawl_stock_info(codes):
    results = []
    for code in codes:
        url = f"https://finance.naver.com/item/main.naver?code={code}"
        html = capture_web_page(url)

        # 크롤링한 결과 저장
        result = {
            '캡처된 내용': html
        }
        results.append(result)

    return results


@app.route('/')
def index():
    codes = [
        "000080", "001430", "011070", "012450", "034020", "042660", "055550", "069960", "074600",
        "141080", "009420", "010140", "011790", "042660", "047050", "352820", "011070", "272290", "001230",
        "006400", "166090", "010690", "013030", "016380", "039030", "043370", "049070", "064290", "067310",
        "082740", "095340", "101360", "108320", "237880", "241710", "298380", "425040", "453340", "456040",
        "460850", "460860", "000150", "000490", "000640", "001120", "001680", "002380", "002790", "003030",
        "003090", "004170", "004800", "005070", "005440", "005850", "006650", "007660", "008770", "009540",
        "010060", "010120", "011760", "012750", "014830", "023160", "031430", "032350", "032640", "033500",
        "034120", "035250", "035900", "036420", "036570", "039130", "042670", "051600", "051900", "052710",
        "053210", "054950", "067160", "069260", "069620", "078930", "090430", "093370", "097520", "101490",
        "103140", "104830", "105630", "112610", "114090", "119610", "120110", "122870", "125210", "126340",
        "131970", "139480", "161890", "178320", "178920", "192820", "195870", "213500", "228670", "237690",
        "241560", "241590", "248070", "267270", "272210", "272450", "284740", "298000", "298040", "298050",
        "306200", "319660", "352480", "353200", "361610", "452260"
    ]
    results = crawl_stock_info(codes)
    html = render_template_string('''
    <!DOCTYPE html>
    <html>
    <head>
        <title>주식 정보</title>
    </head>
    <body>
        <h1>주식 정보</h1>
        {% for result in results %}
            <hr>
            {{ result['캡처된 내용'] | safe }}
            <hr>
        {% endfor %}
    </body>
    </html>
    ''', results=results)

    return html


if __name__ == '__main__':
    app.run()