본문 바로가기
Python/BeautifulSoup

파이썬(BeautifulSoup)사용해서 주식 데이터 크롤링(가져오기)

by 미눅스[멘토] 2023. 7. 8.
728x90
import requests
from bs4 import BeautifulSoup


url = 'https://stock.mk.co.kr/domestic/all_stocks?type=kospi&status=industry'
# url = 'https://stock.mk.co.kr/domestic/all_stocks?type=kosdaq&status=industry'



response = requests.get(url)
# print("response",response.text)

html = response.text
soup = BeautifulSoup(html, 'html.parser')#html형태로 바꾼다.

box = soup.select('.row_sty')


for idx,a in enumerate(box):
    s_name = a.select('a')[0].text.strip()
    # price = a.select('.st_price')[0].text.strip()
    # print(idx,s_code,s_name,price)
    
    
    s_code = a.select('a')[0]['href'].split("/")[3]
    url2 = "https://stock.mk.co.kr/price/home/"+s_code
    response2 = requests.get(url2)
    html = response2.text
    soup2 = BeautifulSoup(html, 'html.parser')#html형태로 바꾼다.
    valume = soup2.select('#trading_valume')[0].text
    acmount = soup2.select('#transaction_acmount')[0].text
    myvolume = soup2.select('.noline_bottom> tbody > tr:nth-child(3) > td:nth-child(2) > span')[0].text.strip()
    stockCount = soup2.select('.noline_bottom > tbody > tr:nth-child(4) > td:nth-child(2) > span')[0].text.strip()
    acount = soup2.select('.noline_bottom> tbody > tr:nth-child(5) > td:nth-child(2) > span')[0].text.strip()
    foreigner = soup2.select('.noline_bottom> tbody > tr:nth-child(6) > td:nth-child(2) > span')[0].text.strip().split("%")[0].strip()
    per = soup2.select('.noline_bottom> tbody > tr:nth-child(7) > td:nth-child(2) > span')[0].text.split("/")[0].strip()
    eps = soup2.select('.noline_bottom> tbody > tr:nth-child(7) > td:nth-child(2) > span')[0].text.split("/")[1].strip()
    
    
    print(s_code ,s_name,"거래량",valume,"거래대금",acmount,"자본금",myvolume,"상장주식주",stockCount,"시가총액",acount,"외국인 보유",foreigner,"PER",per,"EPS",eps)

실행 결과

 

 


센세 코드

import requests
from bs4 import BeautifulSoup

url = 'https://stock.mk.co.kr/price/home/KR7005930003'
# url = 'https://stock.mk.co.kr/domestic/all_stocks?type=kosdak&status=industry'

response = requests.get(url)

html = response.text
soup = BeautifulSoup(html, 'html.parser')
#print("soup", soup)

my_trading_valume = soup.select("#trading_valume")
my_transaction_acmount = soup.select("#transaction_acmount")
my_table_stock = soup.select(".table-stock")
my_vs = my_table_stock[1].select(".volume")

print("거래량", my_trading_valume[0].text)
print("거래대금(백만)", my_transaction_acmount[0].text)
print("자본금(억)", my_vs[2].text.strip())
print("상장주식수(천주)", my_vs[3].text.strip())
print("시가총액(백만)", my_vs[4].text.strip())
print("외국인보유비중", my_vs[5].text.strip().replace("%","").strip())
print("PER", my_vs[6].text.strip().split("/")[0].strip())
print("EPS", my_vs[6].text.strip().split("/")[1].strip())

결과