INVESTAR / StockAnalysisInPython

452 stars 411 forks source link

'finance.naver.com/item/sise_day.nhn?code' issue #175

Open Eligae opened 10 months ago

Eligae commented 10 months ago

9월 1일까지 정상적으로

 url = f"https://finance.naver.com/item/sise_day.nhn?code={code}&page=1"
            html = BeautifulSoup(requests.get(url, headers={'User-agent': 'Mozilla/5.0'}).text, "lxml")
            pgrr = html.find("td", class_="pgRR")
            print(pgrr)
            if pgrr is None:
                return None

이 형식으로 잘 받아오는 것을 확인했는데, 주말부터 이 사이트가 잘못된 주소라고 나옵니다. 코드 값 중 삼성전자 코드값으로 들어가니 접속은 잘 되는데, python에서는 문제가 있는 것으로 보입니다. 아마 컴퓨터 내의 캐시파일이 남아있어서 그런것 같네요.

그래서, 대안으로 https://finance.naver.com/item/sise.naver?code=005930 의 하단에 '일별시세' 탭에서 확인이 가능한데, selenium, request등 다양한 방법을 사용했지만 pgRR의 값을 가져오지 못하는 것으로 보아, 뭔가 문제가 있다고 생각이 듭니다. 어떻게 해야 정상적으로 주식일별시세를 다시 가져올 수 있을까요..?

Eligae commented 10 months ago
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By

# 크롬 드라이버로 웹 페이지 열기
driver = webdriver.Chrome()
driver.get("https://finance.naver.com/item/sise.naver?code=005930")

try:
    wrapper = WebDriverWait(driver, 10).until(
        EC.presence_of_all_elements_located((By.CLASS_NAME, 'pgRR'))
    )
    link_element = wrapper.find_element_by_tag_name('a')
    link = link_element.get_attribute('href')
    print(link)
except TimeoutException:
    print("time out!")

driver.quit()

실행하면 time out!이 실행되는 것을 보아, pgRR을 가져오지 못하는 듯 합니다. 분명 개발자 도구로 확인했을 때


![image](https://github.com/INVESTAR/StockAnalysisInPython/assets/142424611/96177f31-6280-4c17-a793-0395fea81d35)
pgRR에 해당하는 것이 2개 있습니다..