def getPreStockConsenFromHK(stock_code): try: stock_pre_consen_list = [] today = date.today() yesterday = today - timedelta(1) pre_2month = today - timedelta(90) yesterday_str = yesterday.strftime('%Y-%m-%d') pre_2month_str = pre_2month.strftime('%Y-%m-%d') request_url = 'http://hkconsensus.hankyung.com/apps.analysis/analysis.list?sdate='+pre_2month_str+'&edate='+yesterday_str+'&now_page=1&search_value=&report_type=CO&pagenum=50&search_text='+stock_code+'&business_code=' print(request_url) driver=Preference.getWebDriver() driver.get(request_url) table_element = driver.find_element_by_xpath('//*[@id="contents"]/div[2]/table/tbody') tablebody_html = table_element.get_attribute('innerHTML') #print tablebody_html soup = BeautifulSoup(tablebody_html, "html.parser") stock_element_list = soup.find_all('tr') count = 1 for stock_element in stock_element_list: #DATE upload_date=stock_element.td.string stock_data = stock_element.find_all('td') stock_pre_consen ={} i = 1 for astock_data in stock_data: if(i==1): #print astock_data.text stock_pre_consen['update_date']=astock_data.text if(stock_pre_consen['update_date']=='결과가 없습니다.'): break if(i==2): #print astock_data.find('strong').text stock_pre_consen['all_title'] = astock_data.find('strong').text all_title = stock_pre_consen['all_title'] start = all_title.find('(')+1 end = start+6 stock_code = all_title[start:end] stock_pre_consen['stock_code']=stock_code #stockcode=stock_code stock_name = all_title[0:all_title.find('(')] stock_pre_consen['stock_name']=stock_name title = all_title[end+1:len(all_title)] stock_pre_consen['title']=title if(i==3): #print astock_data.text stock_pre_consen['consen_price']=astock_data.text print('###'+stock_pre_consen['consen_price']) if(i==4): #print astock_data.text stock_pre_consen['opinion']=astock_data.text if(i==6): #print astock_data.text stock_pre_consen['analyst_company']=astock_data.text if(i==9): for link in astock_data.find_all('a', href=True): url = link['href'] stock_pre_consen['report_url'] = 'http://hkconsensus.hankyung.com/'+ url #print stock_dic['report_url'] break i=i+1 #print "." #if(stock_pre_consen['consen_price']=='0'): if(stock_pre_consen['update_date']=='결과가 없습니다.'): print('NONE A') elif(stock_pre_consen['stock_code']==''): print('NONE B') else: stock_pre_consen_list.append(stock_pre_consen) count+=1 ''' stock_pre_consen_list.append(stock_pre_consen) count+=1 ''' #print '####'+count #print 'debug4' #print count if (count > Preference.getPrePriceCount()): print(Preference.getPrePriceCount() + ' line limited') break except Exception as e: print('No Pre Consen') print(str(e)) print(stock_pre_consen_list) return stock_pre_consen_list
def getPreStockConsenFromHK(stock_code): #driver = webdriver.PhantomJS(phantomjs, service_args=['--cookies-file=/tmp/cookies.txt']) #driver.delete_all_cookies() ''' opt = webdriver.ChromeOptions() opt.add_argument('headless') opt.add_argument("--disable-gpu") driver = webdriver.Chrome(chromedrive, chrome_options=opt) ''' try: stock_pre_consen_list = [] today = date.today() yesterday = today - timedelta(1) pre_2month = today - timedelta(60) yesterday_str = yesterday.strftime('%Y-%m-%d') pre_2month_str = pre_2month.strftime('%Y-%m-%d') request_url = 'http://hkconsensus.hankyung.com/apps.analysis/analysis.list?sdate=' + pre_2month_str + '&edate=' + yesterday_str + '&now_page=1&search_value=&report_type=CO&pagenum=50&search_text=' + stock_code + '&business_code=' print request_url #driver = webdriver.PhantomJS(phantomjs) driver = Preference.getWebDriver() driver.get(request_url) table_element = driver.find_element_by_xpath( '//*[@id="contents"]/div[2]/table/tbody') tablebody_html = table_element.get_attribute('innerHTML') #print tablebody_html soup = BeautifulSoup(tablebody_html, "html.parser") stock_element_list = soup.find_all('tr') count = 1 for stock_element in stock_element_list: #DATE upload_date = stock_element.td.string stock_data = stock_element.find_all('td') stock_pre_consen = {} i = 1 for astock_data in stock_data: if (i == 1): #print astock_data.text stock_pre_consen['update_date'] = astock_data.text if (i == 3): #print astock_data.text stock_pre_consen['consen_price'] = astock_data.text print '###' + stock_pre_consen['consen_price'] if (i == 4): #print astock_data.text stock_pre_consen['opinion'] = astock_data.text if (i == 6): #print astock_data.text stock_pre_consen['analyst_company'] = astock_data.text if (i == 9): stock_pre_consen['report_url'] = astock_data.text print stock_pre_consen['report_url'] i = i + 1 #print "." if (stock_pre_consen['consen_price'] == '0'): print 'Not Rated' else: stock_pre_consen_list.append(stock_pre_consen) count += 1 #print '####'+count #print 'debug4' #print count if (count > Preference.getPrePriceCount()): print Preference.getPrePriceCount() + ' line limited' break except Exception, e: print 'No Pre Consen' print str(e)