플그래밍/파이써언

[파이썬] 005. 네이버 금융 검색상위 종목 긁어오기

훗티v 2020. 8. 21. 15:42

 

네이버 금융에 들어가보면 검색상위 종목이 정리되있다~

 

 

이렇게~

 

긁어보자~

 

 

복붙복붙~

 

from bs4 import BeautifulSoup
import requests
import re

URL = 'https://finance.naver.com/sise/lastsearch2.nhn'

headers = {
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 OPR/67.0.3575.115'}

page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser', from_encoding="utf8")
stocks = soup.findAll("a", class_="tltle")
prices = soup.findAll("span", class_="tah")

i = 0
if __name__ == '__main__':
    try:
        for stock in stocks:
            print(stock.get_text(), end=" ")
            s = soup.findAll("td", class_="number")
            p = soup.findAll("span", class_="tah")
            # print(p[i].text.strip(), end=" ")
            i += 1
            print("(등락률: " + p[i].text.strip() + ")")
            i += 1

        # for price in prices:
        #     print(price.get_text().strip())
    except BaseException:
        import sys
        print(sys.exc_info()[0])
        import traceback
        print(traceback.format_exc())
    finally:
        print("\n" + "Enter를 누르면 창이 닫혀요 :)")
        input()

 

실행해보면~

 

 

이렇게 :)

 

 

실행파일

naver_popular_stocks.exe
9.62MB

 

 

 

 

 

 

728x90