[python] BeautifulSoup을 이용한 naver 증권 Sector 가져오기

[python] BeautifulSoup을 이용한 naver 증권 Sector 가져오기 updated_at: 2024-01-22 15:00

Naver 섹터별 업체 가져오기

import requests
from bs4 import BeautifulSoup
from utils import *

class Naver():
  def __init__(self):
    super().__init__()

  @property
  def headers(self):
    return {
      "User-Agent": "Mozilla/5.0 (Windows; Windows NT 6.1; rv:2.0b2) Gecko/20100720 Firefox/4.0b2",
      "X-Requested-With": "XMLHttpRequest",
      "Accept": "text/html",
      "Accept-Encoding": "gzip, deflate",
      "Connection": "keep-alive",
    }

  def get_bs_obj(self, url):
    result = requests.get(url, headers=headers())
    bs_obj = BeautifulSoup(result.content.decode('euc-kr', 'replace'),
                           "html.parser")  # html.parser 로 파이썬에서 쓸 수 있는 형태로 변환
    return bs_obj

  def MappingcategorySector(self, surl):
    """
    섹터매핑
    :return:
    """
    url = 'https://finance.naver.com' + surl

    soup = self.get_bs_obj(url)
    tables = soup.find_all("table", attrs={'summary': '업종별 시세 리스트'})
    for t in tables:
      links = t.find_all("a")
      for a in links:
        href = a.attrs['href']
        text = a.string
        try:
          if text:
            print(text, href[-6:])
        except:
          pass
      pass

  def categorySector(self):
    """
    섹터매핑
    :return:
    """
    url = 'https://finance.naver.com/sise/sise_group.naver?type=upjong'
    soup = self.get_bs_obj(url)
    links = soup.find_all("a")

    for a in links:
      href = a.attrs['href']
      text = a.string
      try:
        if 'sise_group_detail.naver' in href:
          print('sector:' + text)
          self.MappingcategorySector(href)
      except:
        pass
    pass

if __name__ == "__main__":
  naver = Naver()
  naver.categorySector()

결과

sector:무역회사와판매업체
포스코인터내셔널 047050
................

sector:철강
포스코엠텍 009520
................

평점을 남겨주세요
평점 : 2.5
총 투표수 : 1

질문 및 답글