-
Python 데이터분석 기초 11 - 일정 시간 마다 웹 문서 읽기Python 데이터 분석 2022. 11. 1. 13:08
# 일정 시간 마다 웹 문서 읽기 # import schedule # pip install schedule 스케쥴러 모듈 지원 import time import datetime import urllib.request as req from bs4 import BeautifulSoup import requests def work(): url = "https://finance.naver.com/marketindex/" # data = req.urlopen(url) # 방법 1, 데이터를 보낼 때 인코딩하여 바이너리 형태로 보낸다. data = requests.get(url).text # 방법 2, 데이터를 보낼 때 딕셔너리 형태로 보낸다. soup = BeautifulSoup(data, 'html.parser') # print(soup) price = soup.select_one('div.head_info > span.value').string print('미국USD :',price) t = datetime.datetime.now() # print(t) fname = './usd/' + t.strftime('%Y-%m-%d-%H-%M-%S') + '.txt' # print(fname) # 파일명 : ./usd/2022-11-01-12-58-09.txt with open(fname, 'w') as f: f.write(price) while True: work() time.sleep(5) <console> 미국USD : 1,420.70 미국USD : 1,420.70 미국USD : 1,420.50
5초마다 한 번씩 읽기 때문에 유동적인 달러가 변동된 상태로 다시 읽혀진다.
BeautifulSoup 사용 방법 및 웹 문서 스크랩핑
참조 사이트 : 영문 https://www.crummy.com/software/BeautifulSoup/bs4/doc/http://dplex.egloos.com/category/Python : BeautifulSoup examplehttp://lxml.de : lxml 라이브러리로 대량의 파일 처리 가능 ---
cafe.daum.net
'Python 데이터 분석' 카테고리의 다른 글
네이버웹툰 html 웹 가져오기 예제 (0) 2022.11.01 Python 데이터분석 기초 12 - 네이버 제공 코스피 정보를 읽어 csv 파일로 저장 (0) 2022.11.01 Python 데이터분석 기초 10 - 웹문서 읽기 (0) 2022.11.01 Python 데이터분석 기초 9 - BeautifulSoup (0) 2022.11.01 Python 데이터분석 기초 8 - pandas(산술연산) (0) 2022.10.31