테스트 환경 : CentOS 7 / MariaDB 10.1.12 / Python 3.5
- 종목의 현재가, 시가총액, 상장주식수, 거래량 추출
한 종목에 대한 전체 데이터는 <tr> </tr>로 묶여있으며 배열 형태로 변수에 저장 srlists=source.find_all('tr')
종목의 세부 데이터는 <td class="number"> </td>로 묶여있음 세부 데이터 중 필요한 데이터는 현재가, 시가총액, 상장주식수, 거래량이므로 해당 데이터만 추출 srlists[i].find_all("td",class_="number")[0].text
srlists[i].find_all("td",class_="number")[4].text
srlists[i].find_all("td",class_="number")[5].text
srlists[i].find_all("td",class_="number")[7].text
|
- Python 소스
import urllib
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = 'http://finance.naver.com/sise/sise_market_sum.nhn?sosok=0&page=1'
html = urlopen(url)
source = BeautifulSoup(html.read())
max_page=source.find_all("table",align="center")
mp = max_page[0].find_all("td",class_="pgRR")
mp_num = int(mp[0].a.get('href')[-2:])
for page in range(1, mp_num+1):
url = 'http://finance.naver.com/sise/sise_market_sum.nhn?sosok=0&page=' + str(page)
html = urlopen(url)
source = BeautifulSoup(html.read())
srlists=source.find_all('tr')
a = None
for i in range(1,len(srlists)-1):
if(srlists[i].a != a):
srlists[i].a.get('href')[-6:]
srlists[i].a.text
srlists[i].find_all("td",class_="number")[0].text
srlists[i].find_all("td",class_="number")[4].text
srlists[i].find_all("td",class_="number")[5].text
srlists[i].find_all("td",class_="number")[7].text
|
- 데이터 출력 확인
종목의 기본 정보에 해당하는 종목코드, 종목명을 기준으로 현재가, 시가총액, 상장주식수, 거래량이 출력됨 |
'Programming' 카테고리의 다른 글
| [Python]주식 종목 데이터 및 일별 시세 데이터 DB적재 (0) | 2016.04.22 |
|---|---|
| Python Parallel로 DB 데이터 읽어오기 (0) | 2016.04.18 |
| Python Parallel 예제 소스 분석 (0) | 2016.04.17 |
| Python Parallel 설치하기 (0) | 2016.04.17 |
| BeautifulSoup4를 이용한 웹페이지 데이터 가져오기[정리필요] (0) | 2016.04.13 |