파이썬 네이버 상한가 찾기 GUI [심화]

Programming/Python|2021. 5. 31. 16:26
728x90
반응형

 

import pandas as pd

import requests

import sys

import datetime

 

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

 

class MyWindow(QMainWindow):

    def __init__(self):

        super().__init__()

 

        # Main Window

        self.setGeometry(0011451024)

        self.setWindowTitle("KOSPI & KOSDAQ 오늘의 상한가 종목")

 

        # KOSPI 버튼

        btn_kospi = QPushButton("KOSPI"self)

        btn_kospi.move(1010)

        btn_kospi.clicked.connect(self.btn_kospi_clicked)        

 

        # KOSPI 테이블

        self.tableWidget_kospi = QTableWidget(self)

        self.tableWidget_kospi.move(1050)        

        self.tableWidget_kospi.resize(1125300)

        self.tableWidget_kospi.setColumnCount(11)

        kospi = ['종목명''현재가''전일비''등락률''거래량''시가''고가''저가''PER''연속''누적']

        self.tableWidget_kospi.setHorizontalHeaderLabels(kospi)

 

        # KOSDAQ 버튼

        btn_kosdaq = QPushButton("KOSDAQ"self)

        btn_kosdaq.move(10380)

        btn_kosdaq.clicked.connect(self.btn_kosdaq_clicked)

 

        # KOSDAQ 테이블

        self.tableWidget_kosdaq = QTableWidget(self)

        self.tableWidget_kosdaq.move(10420)        

        self.tableWidget_kosdaq.resize(1125580)

        self.tableWidget_kosdaq.setColumnCount(11)        

        kosdaq = ['종목명''현재가''전일비''등락률''거래량''시가''고가''저가''PER''연속''누적']

        self.tableWidget_kosdaq.setHorizontalHeaderLabels(kosdaq)       

 

    def btn_kospi_clicked(self):

        html = requests.get('https://finance.naver.com/sise/sise_upper.nhn'

 

        if html != 200

            sys.exit 

 

        table = pd.read_html(html.text

        

        kospi = table[1]         

        kospi = kospi[['종목명''현재가''전일비''등락률''거래량''시가''고가''저가''PER''연속''누적']].dropna(thresh=3)         

        

        #print("코스피 상한가 목록") 

        if kospi.size <= 110:

            kospi_row = int(kospi.size%10)

        else :

            kospi_row = int(kospi.size/10-1)  

 

        self.tableWidget_kospi.setRowCount(kospi_row)              

        

        if kospi.size == 0

            print("없습니다."

        else

            print(kospi.size%10

 

        result_name = 'KOSPI_result' + datetime.datetime.today().strftime("%y%m%d") + '.xlsx' 

        kospi.to_excel(result_namesheet_name='Sheet1')

 

        for i in range(kospi_row):

            for j in range(11):               

                self.tableWidget_kospi.setItem(ijQTableWidgetItem(str(kospi.iat[ij])))

                

    def btn_kosdaq_clicked(self):

        html = requests.get('https://finance.naver.com/sise/sise_upper.nhn'

 

        if html != 200

            sys.exit 

 

        table = pd.read_html(html.text

        

        kosdaq = table[2]        

        print(kosdaq)

        kosdaq = kosdaq[['종목명''현재가''전일비''등락률''거래량''시가''고가''저가''PER''연속''누적']].dropna(thresh=3)

        print(kosdaq)        

        #print("코스닥 상한가 목록") 

        if kosdaq.size <= 110:

            kosdaq_row = int(kosdaq.size%10)

        else :

            kosdaq_row = int(kosdaq.size/10-1)        

 

        self.tableWidget_kosdaq.setRowCount(kosdaq_row)

 

        if kosdaq.size == 0

            print("없습니다."

        else

            print(kosdaq.size

        

        result_name = 'KOSDAQ_result' + datetime.datetime.today().strftime("%y%m%d") + '.xlsx' 

        kosdaq.to_excel(result_namesheet_name='Sheet1')

        

        for i in range(kosdaq_row):            

            for j in range(11):                

                self.tableWidget_kosdaq.setItem(ijQTableWidgetItem(str(kosdaq.iat[ij])))

 

app = QApplication(sys.argv)

window = MyWindow()

window.show()

app.exec_()

 

반응형

댓글()