Spaces:
Sleeping
Sleeping
File size: 3,178 Bytes
8ee5083 9146e95 8ee5083 9146e95 8ee5083 9146e95 8ee5083 9146e95 8ee5083 9146e95 8ee5083 9146e95 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
import gradio as gr
import pandas as pd
import requests
from bs4 import BeautifulSoup
example_tickers = [
{"Ticker": "PLD", "Company Name": "Prologis Inc"},
{"Ticker": "PSA", "Company Name": "Public Storage"},
{"Ticker": "O", "Company Name": "Realty Income Corp"},
{
"Ticker": "META",
"Company Name": "Meta Platforms",
},
{"Ticker": "AMZN", "Company Name": "Amazon.com"},
{"Ticker": "MSFT", "Company Name": "Microsoft Corporation"},
]
def get_esg_from_yahoo_finance(row):
elements = []
# This is a standard user-agent of Chrome browser running on Windows 10
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
}
html = requests.get(
"https://finance.yahoo.com/quote/" + row.Ticker + "/sustainability",
headers=headers,
).text
soup = BeautifulSoup(html, "html.parser")
scores = soup.find_all("div", {"class": "content svelte-y3c2sq"})
for score in scores:
elements.append(float(score.find("h4").text.strip()))
if elements:
row["Total ESG Risk Score"] = elements[0]
row["Environmental Risk Score"] = elements[1]
row["Social Risk Score"] = elements[2]
row["Governance Risk Score"] = elements[3]
else:
row["Total ESG Risk Score"] = None
row["Environmental Risk Score"] = None
row["Social Risk Score"] = None
row["Governance Risk Score"] = None
return row
def get_esg_score_from_google_finance(row):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36"
}
html = requests.get(
f"https://www.google.com/finance/quote/META:NASDAQ", headers=headers, timeout=30
).text
soup = BeautifulSoup(html, "html.parser")
scores = soup.find_all("div", {"class": "IPIeJ"})
row["CDP Score"] = scores[0].find("div", {"class": "P6K39c"}).text
return row
example_input_data = pd.DataFrame(example_tickers)
inputs = [
gr.Dataframe(
row_count=(6, "dynamic"),
col_count=(1, "dynamic"),
label="Input Data",
interactive=1,
)
]
outputs = [
gr.Dataframe(
row_count=(6, "dynamic"),
col_count=(7, "fixed"),
label="ESG Scores",
headers=[
"Ticker",
"Company Name",
"Total ESG Risk Score",
"Environmental Risk Score",
"Social Risk Score",
"Governance Risk Score",
"CDP Score",
],
)
]
def get_esg_scores(input_dataframe):
input_dataframe = input_dataframe.apply(
lambda x: get_esg_from_yahoo_finance(x), axis=1
)
input_dataframe = input_dataframe.apply(
lambda x: get_esg_score_from_google_finance(x), axis=1
)
return input_dataframe
gr.Interface(
fn=get_esg_scores,
inputs=inputs,
outputs=outputs,
title="🌳ESG Data Scraper🌳\n\nIt scrapes ESG ratings from Yahoo Finance and Google Finance!",
examples=[[example_input_data.head(6)]],
).launch()
|