chromedriver download
https://googlechromelabs.github.io/chrome-for-testing/
에서 자신의 chrome 브라우저와 버전이 같은 chromedriver 다운로드
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time
import paramiko
import os
# SFTP 접속 정보
SFTP_HOST = '192.168.0.25'
SFTP_PORT = 22 # 기본 22
SFTP_USER = 'userid'
SFTP_PASSWORD = 'userpw' # 키 인증 사용 시 password 대신 key 사용 가능
REMOTE_DIR = '/home/prmonitor/www/' # 서버에서 저장할 경로
LOCAL_DIR = r'D:\git\cloud\files' # 업로드할 로컬 폴더
def upload_folder(local_dir, remote_dir):
transport = paramiko.Transport((SFTP_HOST, SFTP_PORT))
transport.connect(username=SFTP_USER, password=SFTP_PASSWORD)
sftp = paramiko.SFTPClient.from_transport(transport)
# 폴더 내 모든 파일 업로드
for filename in os.listdir(local_dir):
local_file = os.path.join(local_dir, filename)
remote_file = os.path.join(remote_dir, filename)
if os.path.isfile(local_file):
sftp.put(local_file, remote_file)
print(f"Uploaded {local_file} -> {remote_file}")
sftp.close()
transport.close()
def fetch_bloomberg_html(keyword, page=1, headless=True):
# 검색 URL
url = f"https://www.bloomberg.com/search?query={keyword}&sort=relevance"
# Chrome 옵션 설정
chrome_options = Options()
chrome_options.add_argument("--headless=new") # 최신 headless 모드
chrome_options.add_argument("--no-sandbox") # root 계정 필수
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=1920,1080") # 화면 크기 설정
chrome_options.add_argument(
"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/120.0.0.0 Safari/537.36"
)
# ChromeDriver 경로
service = Service("D:\\git\\cloud\\chromedriver.exe")
driver = webdriver.Chrome(service=service, options=chrome_options)
try:
driver.get(url)
# 페이지 로딩 대기 (JavaScript 렌더링)
time.sleep(5) # 필요 시 WebDriverWait으로 더 정밀하게 설정 가능
# HTML 가져오기
html = driver.page_source
return html
finally:
driver.quit()
if __name__ == "__main__":
keywords = ["Standard+Chartered", "StanChart", "ELS+penalties"]
for keyword in keywords:
html = fetch_bloomberg_html(keyword, headless=True)
filename = keyword+".html"
with open(LOCAL_DIR+"\\"+filename, "w", encoding="utf-8") as f:
f.write(html)
print(f"✅ HTML 저장 완료: {filename}")
upload_folder(LOCAL_DIR, REMOTE_DIR)'Python' 카테고리의 다른 글
| tkinter 실행파일(exe) 배포 (0) | 2022.12.21 |
|---|---|
| 우분투 (Ubuntu) 에서 다른 버전의 python 사용하기 (1) | 2022.09.20 |
| pip 설치된 라이브러리 확인, 특정 버전 설치 (0) | 2022.04.11 |
| selenium 4 에서 바뀐 부분들 (0) | 2022.03.24 |
| Centos 8 python3.6 삭제 하고 python3.9 설치 (0) | 2022.03.22 |