티스토리 뷰

이전글에서 파이썬 설치했고 간단한 출력도 해보았으니 바로 스크래핑하여 네이버 종합주가지수를 가져와 보도록 하자

#content > div.article > div.section2 > div.section_stock_market > div.section_stock > div.kospi_area.group_quot.quot_opn > div.heading_area > a > span > span.num
import requests
from bs4 import BeautifulSoup

#requests 설치
# Terminal > pip install requests

# 1. requests로 html을 통채로 받아온다.
res = requests.get("https://finance.naver.com/")
#print(res.content)

# 2. beautifulsoup 으로 파싱가능한 상태로 만들어준다.
# pip install beautifulsoup4
soup = BeautifulSoup(res.content, 'html.parser')
#print(soup)

# 3. css selector 문법을 원하는 데이터를 가져온다. (끝)
the_tag = soup.select_one("#content > div.article > div.section2 > div.section_stock_market > div.section_stock > div.kospi_area.group_quot.quot_opn > div.heading_area > a > span > span.num")
print(the_tag.text)

출력결과:

2,582.18

Process finished with exit code 0

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함