def get_estimates(stock: str): # Convert Stock Name to Lowercase stock = stock.lower() # Load Data soup = _make_soup("https://www.finanzen.net/schaetzungen/" + stock) # Check for Error _check_site(soup) # Parse Table containing Yearly Estimates table_dict = {} table = soup.find("h1", text=re.compile("^Schätzungen")).parent years = table.find_all("th")[1:] years = [x.get_text() for x in years] rows = table.find_all("tr")[1:] for row in rows: fields = row.find_all("td") fields = [x.get_text() for x in fields] name = fields[0] row_data = fields[1:] row_data = [x if x != "-" else None for x in row_data] row_data = [ re.sub("[^\d,]", "", x) if x is not None else x for x in row_data ] row_data = [ re.sub(",", ".", x) if x is not None else x for x in row_data ] row_data = [float(x) if x is not None else x for x in row_data] table_dict[name] = dict(zip(years, row_data)) # Return Estimates in Dict return table_dict
def get_estimates(stock: str, output="dataframe"): # Check Input if output not in ["dataframe", "dict"]: raise ValueError( "Please choose either 'dict' or 'dataframe' for input") # Convert Stock Name to Lowercase stock = stock.lower() # Load Data soup = _make_soup("https://www.finanzen.net/schaetzungen/" + stock) # Check for Error _check_site(soup) # Parse Table containing Yearly Estimates table_dict = {} table = soup.find("h1", text=re.compile("^Schätzungen")).parent years = table.find_all("th")[1:] years = [x.get_text() for x in years] rows = table.find_all("tr")[1:] for row in rows: fields = row.find_all("td") fields = [x.get_text() for x in fields] name = fields[0] row_data = fields[1:] row_data = [x if x != "-" else None for x in row_data] row_data = [ re.sub("[^\d,]", "", x) if x is not None else x for x in row_data ] row_data = [ re.sub(",", ".", x) if x is not None else x for x in row_data ] row_data = [float(x) if x is not None else x for x in row_data] table_dict[name] = dict(zip(years, row_data)) # Return Estimates if output == "dict": return table_dict else: df_list = [] for f in table_dict: df_tmp = pd.DataFrame({ "Metric": f, "Year": list(table_dict[f].keys()), "Value": list(table_dict[f].values()) }) df_list.append(df_tmp) return pd.concat(df_list)
def search_stock(stock: str, limit: int = -1): # Convert Stock Name to Lowercase stock = stock.lower() # Make Request soup = _make_soup("https://www.finanzen.net/suchergebnis.asp?_search=" + stock) # Check for Error if soup.find("div", {"class": "red"}) is not None: if "kein Ergebnis geliefert" in soup.find("div", { "class": "red" }).get_text(): return list() # Define Function to Extract Results result_list = [] table_outer_div = soup.find("div", {"class": "table-responsive"}) table = table_outer_div.find("table", {"class": "table"}) rows = table.find_all("tr") for row in rows[1:]: cells = row.find_all("td") name = cells[0].get_text() link = cells[0].find("a")["href"] link = "https://www.finanzen.net" + link isin = cells[1].get_text() wkn = cells[2].get_text() result_list.append((name, link, isin, wkn)) # Filter Result if limit was given if limit > 0: # Decrease limit if bigger than result result_len = len(result_list) if limit > result_len: limit = result_len result_list = result_list[0:limit] # Return Result List as formatted String names = [] for result in result_list: stock_name = result[0] short_name = re.search("aktien/(.+)-aktie", result[1]).group(1) names.append({ 'name': stock_name, 'short_name': short_name, 'isin': result[2], 'wkn': result[3] }) return names
def get_etf_info(stock, output="dict"): # Parse User Input if output not in ["dict"]: raise ValueError("Only 'dict' supportet for now") # Load Data soup = _make_soup("https://www.finanzen.net/etf/" + stock) WKN = soup.find("span", text="WKN:").next_sibling.strip() ISIN = soup.find("span", text="ISIN:").next_sibling.strip() currentStockTable = soup.find("div", class_="table-responsive quotebox").table currentStockPrice = float(currentStockTable.tr.td.contents[0].replace( ",", ".")) currentStockCurrency = currentStockTable.tr.td.span.contents[0] currentStockExchange = currentStockTable.find( "div", class_="quotebox-time").find_next_sibling("div").next_element.strip() baseDataTable = soup.find("h2", text="Wichtige Stammdaten").parent baseDataIssuer = baseDataTable.find("div", text="Emittent").parent.a.text baseDataBenchmark = baseDataTable.find("div", text="Benchmark").parent.find( "div", { "title": True }).text baseDataFondsSize = float( baseDataTable.find("div", text="Fondsgröße").parent.find( "div", { "title": True }).text.replace(".", "").replace(",", ".")) info = { "currentStockPrice": currentStockPrice, "currentStockCurrency": currentStockCurrency, "currentStockExchange": currentStockExchange, "WKN": WKN, "ISIN": ISIN, "Issuer": baseDataIssuer, "Benchmark": baseDataBenchmark, "FondsSize": baseDataFondsSize } if output == "dict": return info
def get_fundamentals(stock: str, output="dataframe"): # Parse User Input if output not in ["dataframe", "dict"]: raise ValueError( "Please choose either 'dict' or 'dataframe' for input") # Convert name to lowercase stock = stock.lower() # Load Data soup = _make_soup("https://www.finanzen.net/bilanz_guv/" + stock) # Check for Error _check_site(soup) # Define Function to Parse Table def _parse_table(soup, signaler: str): table_dict = {} table = soup.find("h2", text=re.compile(signaler)).parent years = [int(x.get_text()) for x in table.find_all("th")[2:]] rows = table.find_all("tr")[1:] for row in rows: name = row.find("td", {"class": "font-bold"}).get_text() row_data = row.find_all("td") row_data = row_data[2:] row_data = [x.get_text() for x in row_data] row_data = [re.sub(r"\.", "", x) for x in row_data] row_data = [re.sub(",", ".", x) for x in row_data] row_data = [float(x) if x != "-" else None for x in row_data] table_dict[name] = dict(zip(years, row_data)) return table_dict # Extract Stock Quote Info+ try: quote_info = _parse_table(soup, "Die Aktie") except Exception: quote_info = None # Extract Key Ratios try: key_ratios = _parse_table(soup, "Unternehmenskennzahlen") except Exception: key_ratios = None # Extract Income Statement try: income_info = _parse_table(soup, "GuV") except Exception: income_info = None # Extract Balance Sheet try: balance_sheet = _parse_table(soup, "Bilanz") except Exception: balance_sheet = None # Extract Other Information try: other_info = _parse_table(soup, "sonstige Angaben") except Exception: other_info = None # Collect Fundamentals into single Directory fundamentals = { "Quotes": quote_info, "Key Ratios": key_ratios, "Income Statement": income_info, "Balance Sheet": balance_sheet, "Other": other_info } # Return Fundamentals if output is set to dict if output == "dict": return fundamentals else: df_list = [] for f in fundamentals: for i in fundamentals[f]: df_tmp = pd.DataFrame({ "Category": f, "Metric": i, "Year": list(fundamentals[f][i].keys()), "Value": list(fundamentals[f][i].values()) }) df_list.append(df_tmp) fundamentals_df = pd.concat(df_list) return fundamentals_df
def get_fundamentals(stock: str): # Convert name to lowercase stock = stock.lower() # Load Data soup = _make_soup("https://www.finanzen.net/bilanz_guv/" + stock) # Check for Error _check_site(soup) # Define Function to Parse Table def _parse_table(soup, signaler: str): table_dict = {} table = soup.find("h2", text=re.compile(signaler)).parent years = [x.get_text() for x in table.find_all("th")[2:]] rows = table.find_all("tr")[1:] for row in rows: name = row.find("td", {"class": "font-bold"}).get_text() row_data = row.find_all("td") row_data = row_data[2:] row_data = [x.get_text() for x in row_data] row_data = [re.sub(r"\.", "", x) for x in row_data] row_data = [re.sub(",", ".", x) for x in row_data] row_data = [float(x) if x != "-" else None for x in row_data] table_dict[name] = dict(zip(years, row_data)) return table_dict # Extract Stock Quote Info+ try: quote_info = _parse_table(soup, "Die Aktie") except Exception: quote_info = None # Extract Key Ratios try: key_ratios = _parse_table(soup, "Unternehmenskennzahlen") except Exception: key_ratios = None # Extract Income Statement try: income_info = _parse_table(soup, "GuV") except Exception: income_info = None # Extract Balance Sheet try: balance_sheet = _parse_table(soup, "Bilanz") except Exception: balance_sheet = None # Extract Other Information try: other_info = _parse_table(soup, "sonstige Angaben") except Exception: other_info = None # Collect Fundamentals into single Directory fundamentals = { "Quotes": quote_info, "Key Ratios": key_ratios, "Income Statement": income_info, "Balance Sheet": balance_sheet, "Other": other_info } # Return Fundamentals return fundamentals