티스토리 뷰
Python으로 BeautifulSoup4를 사용해서 웹크롤링 해보자.
예제로 Naver 배당주 페이지를 크롤링 해보자
※ BeautifulSoup4 설치
pip install BeautifulSoup4
※ 소스
import requests
from bs4 import BeautifulSoup as bs
arr = []
for i in range(1, 28): #28
page = requests.get("https://finance.naver.com/sise/dividend_list.naver?field=dividend_rate&sosok=&ordering=desc&page=" + str(i))
soup = bs(page.text, "html.parser")
elements = soup.select('table.type_1 tr td a')
# append() [-6:] strip() replace() float() .parent
for index, element in enumerate(elements, 1):
a = element
b = a.parent.parent.select('td')
arr2 = [a.attrs['href'][-6:], a.text]
for i in range(1,12):
if b[i].text.strip() != '-':
arr2.append(float(b[i].text.strip().replace(',','')))
else:
arr2.append('')
arr.append(arr2)
print(arr)
※ 실행
728x90
'Software > Python' 카테고리의 다른 글
Python 시작하기 - selenium 활용 (0) | 2024.07.02 |
---|---|
Python 시작하기 - selenium 소개 (0) | 2024.07.02 |
Python 시작하기 - JSON 변환 (0) | 2024.06.23 |
Python 시작하기 - CSV 저장 (0) | 2024.06.23 |
Python 시작하기 - Mysql 조회 (0) | 2024.06.23 |
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- pyside6
- JavaScript
- c#
- Winform
- 바둑판
- Canvas
- vscode
- 웹크롤링
- png2ico
- game
- Python
- 포트 포워덩
- MSX
- maze
- 디지털시계
- MySQL
- PyQtGraph
- web
- svg
- plot
- 3d
- 초중종분리
- Java
- php
- csv
- AI
- QT
- JSON
- 미로
- Selenium
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
글 보관함