File size: 3,919 Bytes
8ee5083
 
9146e95
 
 
 
db5b570
 
 
 
 
9146e95
8ee5083
 
9146e95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db5b570
 
 
 
 
 
9146e95
 
 
 
db5b570
9146e95
 
 
db5b570
 
 
9146e95
 
 
db5b570
 
9146e95
db5b570
 
 
9146e95
 
 
 
 
 
 
 
 
db5b570
 
9146e95
 
db5b570
 
 
 
 
9146e95
 
8ee5083
9146e95
 
db5b570
 
9146e95
 
 
 
db5b570
9146e95
 
 
 
db5b570
9146e95
db5b570
 
 
 
 
 
 
 
 
 
 
 
 
9146e95
 
 
8ee5083
 
9146e95
 
 
 
 
 
 
8ee5083
9146e95
8ee5083
 
9146e95
 
 
 
01ba592
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import gradio as gr
import pandas as pd
import requests
from bs4 import BeautifulSoup

example_tickers = [
    {"Ticker": "PLD", "Company Name": "Prologis Inc", "Market": "NYSE"},
    {"Ticker": "PSA", "Company Name": "Public Storage", "Market": "NYSE"},
    {"Ticker": "META", "Company Name": "Meta Platforms", "Market": "NASDAQ"},
    {"Ticker": "MSFT", "Company Name": "Microsoft Corporation", "Market": "NASDAQ"},
    {"Ticker": "AMZN", "Company Name": "Amazon.com", "Market": "NASDAQ"},
]


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

    row["Yahoo Finance Link"] = (
        f"[Yahoo Link](https://finance.yahoo.com/quote/"
        + row.Ticker
        + "/sustainability)"
    )
    return row


def get_esg_score_from_google_finance(row):
    print(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"
    }
    link = f"https://www.google.com/finance/quote/{row.Ticker}:{row.Market}"

    html = requests.get(link, headers=headers, timeout=30).text

    soup = BeautifulSoup(html, "html.parser")
    scores = soup.find_all("div", {"class": "IPIeJ"})
    if scores:
        row["CDP Score"] = scores[0].find("div", {"class": "P6K39c"}).text

    else:
        row["CDP Score"] = None
    row["Google Finance Link"] = f"[Google Finance Link]({link})"

    return row


example_input_data = pd.DataFrame(example_tickers)


inputs = [
    gr.Dataframe(
        row_count=(5, "dynamic"),
        col_count=(3, "dynamic"),
        label="Input Data",
        interactive=1,
        headers=[
            "Ticker",
            "Company Name",
            "Market",
        ],
    )
]

outputs = [
    gr.Dataframe(
        row_count=(5, "dynamic"),
        col_count=(10, "fixed"),
        label="ESG Scores",
        headers=[
            "Ticker",
            "Company Name",
            "Market",
            "Total ESG Risk Score",
            "Environmental Risk Score",
            "Social Risk Score",
            "Governance Risk Score",
            "Yahoo Finance Link",
            "CDP Score",
            "Google Finance Link",
        ],
        datatype=[
            "str",
            "str",
            "str",
            "str",
            "str",
            "str",
            "str",
            "markdown",
            "str",
            "markdown",
        ],
    )
]


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🌳 It scrapes ESG ratings from Yahoo Finance and Google Finance!",
    examples=[[example_input_data.head(6)]],
).launch()