spacepaste

  1.  
  2. from selenium import webdriver
  3. from selenium.webdriver.common.keys import Keys
  4. def yah(sym):
  5. ''' Scrape Yahoo Finance '''
  6. chromedriver = "/Applications/chromedriver"
  7. os.environ["webdriver.chrome.driver"] = chromedriver
  8. driver = webdriver.Chrome(chromedriver)
  9. key = 'http://finance.yahoo.com/quote/'+sym+'/key-statistics?p='+sym
  10. hold = 'http://finance.yahoo.com/quote/'+sym+'/holders?p='+sym
  11. # Scrape data from key statistics page
  12. driver.get(key)
  13. cash_sel = '//*[@id="main-0-Quote-Proxy"]/section/div[2]/section/div/' \
  14. + 'section/div[2]/div[1]/div[2]/div[5]/table/tbody/tr[1]/td[2]'
  15. debt_sel = '//*[@id="main-0-Quote-Proxy"]/section/div[2]/section/div/' \
  16. + 'section/div[2]/div[1]/div[2]/div[5]/table/tbody/tr[3]/td[2]'
  17. ebitda_sel = '//*[@id="main-0-Quote-Proxy"]/section/div[2]/section/div/' \
  18. + 'section/div[2]/div[1]/div[2]/div[4]/table/tbody/tr[5]/td[2]'
  19. beta_sel = '//*[@id="main-0-Quote-Proxy"]/section/div[2]/section/' \
  20. + 'div/section/div[2]/div[2]/div/div[1]/table/tbody/tr[1]/td[2]'
  21. cash = mb(driver.find_element_by_xpath(cash_sel).text)
  22. debt = mb(driver.find_element_by_xpath(debt_sel).text)
  23. ebitda = mb(driver.find_element_by_xpath(ebitda_sel).text)
  24. beta = bet(driver.find_element_by_xpath(beta_sel).text)
  25. # Scrape data from holders page
  26. driver.get(hold)
  27. inside_sel = '//*[@id="main-0-Quote-Proxy"]/section/div[2]/section/div/' \
  28. + 'section/div[3]/div[2]/div[1]/table/tbody/tr[1]/td[1]'
  29. inst_sel = '//*[@id="main-0-Quote-Proxy"]/section/div[2]/section/div/' \
  30. + 'section/div[3]/div[2]/div[1]/table/tbody/tr[2]/td[1]'
  31. inside = pct(driver.find_element_by_xpath(inside_sel).text)
  32. inst = pct(driver.find_element_by_xpath(inst_sel).text)
  33. return (cash, debt, ebitda, beta, inside, inst)
  34. for t in tqdm(ticks):
  35. try:
  36. yaht = yah(t)
  37. cash.append(yaht[0])
  38. debt.append(yaht[1])
  39. ebitda.append(yaht[2])
  40. beta.append(yaht[3])
  41. inside.append(yaht[4])
  42. inst.append(yaht[5])
  43. ticky.append(t)
  44. except:
  45. continue
  46.