Skip to content

Instantly share code, notes, and snippets.

@shotahorii
Created May 29, 2017 23:40
Show Gist options
  • Save shotahorii/68e4fff72b3404d0036e802e455a7ca0 to your computer and use it in GitHub Desktop.
Save shotahorii/68e4fff72b3404d0036e802e455a7ca0 to your computer and use it in GitHub Desktop.
A python module containing functions that convert cik to ticker & ticker to cik
import re
from requests import get
from bs4 import BeautifulSoup
# this is based on https://gist.github.com/dougvk/8499335
def ticker2cik(ticker):
URL = 'http://www.sec.gov/cgi-bin/browse-edgar?CIK={}&Find=Search&owner=exclude&action=getcompany'
CIK_RE = re.compile(r'\d{10}')
soup = BeautifulSoup(get(URL.format(ticker)).content, "lxml")
for span in soup.find_all('span',{'class':'companyName'}):
matched = CIK_RE.findall(span.text)
if len(matched): return str(matched[0])
return '0000000000'
def cik2ticker(cik):
URL = 'http://yahoo.brand.edgar-online.com/default.aspx?cik={}'
RE_EXP = re.compile(r':\s+[\w\.\-\^]+\s')
soup = BeautifulSoup(get(URL.format(cik)).content, "lxml")
for td in soup.find_all('td',{'class':'tableTop'}):
matched = RE_EXP.findall(td.text)
if len(matched): return str(matched[0][1:].strip())
return '0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment