티스토리 뷰

반응형

주식 관련한 프로그래밍을 하려면 필수적으로 주가/지수등의 데이터 수집이 필요한데요.

주로 크롤링 하는 곳이 investing.com 입니다.

https://kr.investing.com/?ref=www 

 

주식시장 시세와 금융뉴스 - Investing.com

인베스팅닷컴(Investing.com)은 실시간 시세, 포트폴리오, 챠트, 최신 금융 뉴스, 실시간 주식시장 데이터를 무료로 제공하고 있습니다. 투자자들이 포트폴리오에 담은 종목 관련 뉴스와 글로벌 경

kr.investing.com

 

다음은 크롤링을 하기 위한 파이썬 라이브러리 입니다.

그러나 2022년 10월경 부터 인증 강화로 인해 아래 라이브러리로 접근시 403 오류가 발생합니다.(해결책은 아래에...)

https://github.com/alvarobartt/investpy

 

GitHub - alvarobartt/investpy: Financial Data Extraction from Investing.com with Python

Financial Data Extraction from Investing.com with Python - GitHub - alvarobartt/investpy: Financial Data Extraction from Investing.com with Python

github.com

 

[샘플]

import investpy
...생략...

cur_time = round(time.time())
url = f"https://tvc4.investing.com/784558eb27ba23137f73ad411da3a98b/{cur_time}/1/1/8/history"
symbol = row['id']
resolution = '1'
if symbol == 942611:
    resolution = '5'

time_from = self._get_last(row['id'], interval='Minute')
time_to = cur_time
print(datetime.fromtimestamp(time_from).strftime('%Y-%m-%d %H:%M:%S'))
print(datetime.fromtimestamp(time_to).strftime('%Y-%m-%d %H:%M:%S'))
params = {'symbol': symbol,
          'resolution': resolution,
          'from': time_from,
          'to': time_to}
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
                  'like Gecko) '
                  'Chrome/92.0.4515.131 Safari/537.36',
    'Content-Type': 'text/plain'}
r = requests.get(url=url, params=params, headers=headers)
data = r.json()
df = pd.DataFrame.from_dict(data)

 

[해결책]

아래와 같이 request.get()의 url 부분을 다른 사이트로 경유하는 방법입니다.

api.scraperlink.com 는 investing에서 요구하는 사용자 인증을 대신 처리해 주는 사이트 입니다.

 

초기에는 이메일을 체크하지 않았으나, 최근(2023년 7월)에는 이메일을 정확하게 넣어야 하며, 인증 메일이 오면 인증 처리를 해야지만 정상 동작을 합니다.

# r = requests.get(url=url, params=params, headers=headers)

r = requests.get(
    url=f"http://api.scraperlink.com/investpy/?email=test@gmail.com&url={url}?from={time_from}%26to={time_to}%26resolution={resolution}%26symbol={symbol}")

프로그램 소스를 실행하면 아래와 같이 메일이 발송되는데 URL 부분을 복사해서 웹브라우저에 넣고 엔터키를 하면 인증이 완료 됩니다.

Please verify your email address: https://api.scraperlink.com/verify/?email=woohaeng@gmail.com&key=xxxxxxxxxxxxxxxxxxxxxxx

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