***** 오늘의 테마 주도주 크롤링 ******

마켓 크롤링 리포트가 오늘의 시장의 이슈를 반영을 못하는 듯 한 느낌이 일주일만에 들어서 오늘의 테마를 가져오는 크롤링 코드를 하나 만들었다.

1. 네이버 증권에서 테마 화면으로 진입하여 html 소스를 검토한다.

테마별 시세 : 네이버 금융 (naver.com)

페이지의 구조를 살펴보면 아래와 같다.

텍스트이(가) 표시된 사진  자동 생성된 설명

2. 크롤링 구조 계획

(1)금일의 테마를 순서대로 가져오기

Table tag의 class가 “type1 theme” 를 찾고 하위 태그 중 td의 class가 “col_type1”을 모두 찾아서 리스트에 저장하면 오늘의 테마 순위대로 리스트에 쌓인다.

(2) 첫번째 주도주 가져오기

Table tag의 class가 “type1 theme” 를 찾고 하위 태그 중 td의 class가 “ls col_type5”을 모두 찾아서 리스트에 저장하면 오늘의 각 테마에서 첫번째 주도주가 리스트에 쌓인다. 이때 a tag의 href attribute 값까지 가져오면 해당 종목 링크까지 리스트에 쌓게 할 수 있다.

(3) 두번째 주도주 가져오기

Table tag의 class가 “type1 theme” 를 찾고 하위 태그 중 td의 class가 “ls col_type6”을 모두 찾아서 리스트에 저장하면 오늘의 각 테마에서 첫번째 주도주가 리스트에 쌓인다. 이때 a tag의 href attribute 값까지 가져오면 해당 종목 링크까지 리스트에 쌓게 할 수 있다.

3. 코드

def get_soup_obj(url):
    headers = {'User-Agent': 'Mozilla/5.0 \
            (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
             KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36'}
    res = requests.get(url, headers=headers)
    soup = BeautifulSoup(res.text, 'lxml')
    return soup

def getThemeList( themeNameList:List, itemList1:List, itemLinkList1:List, itemList2:List, itemLinkList2:List):
    url = 'https://finance.naver.com/sise/theme.naver'
    soup = get_soup_obj(url)
    themeName = soup.find('table', class_='type_1 theme').find_all('td', class_= 'col_type1')


    for a in themeName:
        themeNameList.append(a.a.text)


    tem1 = soup.find('table', class_='type_1 theme').find_all('td', class_= 'ls col_type5')


    for a in item1:
        itemList1.append(a.a.text)
        itemLinkList1.append("https://finance.naver.com/"+ a.a.attrs.get('href'))




    item2 = soup.find('table', class_='type_1 theme').find_all('td', class_= 'ls col_type6')
    for a in item2:
        itemList2.append(a.a.text)
        itemLinkList2.append("https://finance.naver.com/"+ a.a.attrs.get('href'))
 
   

+ Recent posts