mmyjh86

[러닝-인강] 한경컨센서스

Oct 3rd, 2020
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import requests
  2. from bs4 import BeautifulSoup
  3.  
  4. def get_report_info(시작일, 종료일, 리포터) :
  5.     url = "http://consensus.hankyung.com/apps.analysis/analysis.list"
  6.  
  7.     params = {
  8.         "sdate":시작일,
  9.         "edate": 종료일,
  10.         "now_page":1,
  11.         "report_type":"CO",
  12.         "pagenum":20,
  13.         "search_text": 리포터.encode('euc-kr')
  14.     }
  15.  
  16.     headers = {
  17.         "User-Agent": "Hi"
  18.     }
  19.  
  20.     resp = requests.get(url, headers=headers, params=params)
  21.     resp.encoding = 'euc-kr'
  22.     soup = BeautifulSoup(resp.text, 'html5lib')
  23.  
  24.     일자 = [ ]
  25.     sel = "#contents > div.table_style01 > table > tbody > tr > td:nth-child(1)"
  26.     result = soup.select(sel)
  27.     for item in result:
  28.         일자.append(item.text.strip())
  29.  
  30.     제목 = [ ]
  31.     sel = "#contents > div.table_style01 > table > tbody > tr > td:nth-child(2)"
  32.     result = soup.select(sel)
  33.     for item in result:
  34.         제목.append(item.text.strip())
  35.  
  36.     투자의견 = []
  37.     sel = "#contents > div.table_style01 > table > tbody > tr > td:nth-child(4)"
  38.     result = soup.select(sel)
  39.     for item in result:
  40.         투자의견.append(item.text.strip())
  41.  
  42.     return [  일자, 제목, 투자의견  ]
  43.  
  44. def get_earning_rate (ticker, open_year, open_mon, open_day):
  45.     url = f"http://consensus.hankyung.com/apps.chart/chart.chartList?report_type=CO&business_code={ticker}"
  46.     headers = {"user-agent": "hi"}
  47.     resp = requests.get(url, headers=headers)
  48.     r = resp.text
  49.  
  50.     r = r.replace("[,", "")
  51.     r = r.replace(" \n", "")
  52.     r = r.replace("jQuery112407887789095301501_1590834572386", "")
  53.     r = r.replace("Date.UTC", "")
  54.     r = r.replace("[", "")
  55.     r = r.replace("]", "")
  56.     r = r.replace("(", "")
  57.     r = r.replace(")", "")
  58.     r = r.replace('null', '0')
  59.     r = r.split(",")
  60.     for i in range(0, len(r), 4):
  61.         year = int(r[i])
  62.         mon = int(r[i+1]) + 1
  63.         day = int(r[i+2]) - 1
  64.         val = int(r[i+3])
  65.  
  66.         if year == open_year and mon == open_mon and day == open_day :
  67.             시가 = val
  68.             종가 = int(r[-1])
  69.             return (종가 / 시가 - 1) * 100
  70.  
  71.  
  72.  
  73. def print_earning_rate(시작일, 종료일, reporter):
  74.     result = get_report_info(시작일, 종료일, reporter)
  75.     for idx in range( len(result[0]) ) :
  76.  
  77.         if result[2][idx] != "Buy" :
  78.             continue
  79.  
  80.         제목 = result[1][idx]
  81.  
  82.         if '(' not in 제목:
  83.             continue
  84.  
  85.         start_index = 제목.index('(')
  86.         ticker = 제목[start_index+1 : start_index+7 ]
  87.         open_year = int(result[0][idx][:4])
  88.         open_mon  = int(result[0][idx][5:7])
  89.         open_day  = int(result[0][idx][8:10])
  90.  
  91.         수익률 = get_earning_rate(ticker, open_year, open_mon, open_day)
  92.         print(ticker, 수익률)
  93.  
  94.  
  95. print_earning_rate('2020-01-01', '2020-01-20', '최준영')
  96. print_earning_rate('2020-01-01', '2020-01-20', '김평모')
  97. print_earning_rate('2020-01-01', '2020-01-20', '신은정')
  98. print_earning_rate('2020-01-01', '2020-01-20', '주영훈')
RAW Paste Data