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())
'Python > BeautifulSoup' 카테고리의 다른 글
[크롤링] 파이썬으로 간단하게 크롤링하기3. (한개의 상품 크롤링) (0) | 2024.10.08 |
---|---|
[크롤링] 파이썬으로 간단하게 크롤링하기2. (Beautifulsoup4, request라이브러리 사용법) (1) | 2024.10.07 |
[크롤링] 파이썬으로 간단하게 크롤링하기1. (환경설정) (0) | 2024.10.07 |
파이썬(BeautifulSoup) 원하는 데이터 크롤링 하기 (0) | 2023.07.07 |
파이썬(BeautifulSoup) 크롤링 하기위한 뷰티풀 수프 다운 (0) | 2023.07.07 |