Fix MarketBeat disc/ratings scraping. (#584)

* Add line to mention saved generated report on terminal

* Fix MarketBeat scrap ratings
This commit is contained in:
DidierRLopes
2021-08-01 01:58:05 +01:00
committed by GitHub
parent 4e264977f3
commit a0acdfb13f
6 changed files with 96 additions and 48 deletions

View File

@@ -71,23 +71,23 @@ def get_ratings() -> List[dict]:
td = tds[3]
if td.find("span"):
td.span.extract()
current_price = td.text
rating["current_price"] = current_price
analyst = td.text
rating["analyst"] = analyst
# target price
td = tds[4]
target_price = td.text
rating["target_price"] = target_price
current_price = td.text
rating["current_price"] = current_price
# new rating
td = tds[5]
new_rating = td.text
rating["rating"] = new_rating
target_price = td.text
rating["target_price"] = target_price
# impact on price
td = tds[6]
if td.find("a"):
impact = td.find("a").text
rate = td.find("a").text
else:
impact = td.text
rating["impact"] = impact
rate = td.text
rating["rate"] = rate
ratings.append(rating)
return ratings

View File

@@ -14,7 +14,10 @@ from gamestonk_terminal.discovery import marketbeat_model
def ratings_view(other_args: List[str]):
"""Prints top ratings updates
"""Prints top ratings updates [Source: MarketBeat]
MarketBeat has changed the access to their data. Now, a user needs to have 'MarketBeat All Access'
to make the most out of this command.
Parameters
----------
@@ -25,7 +28,9 @@ def ratings_view(other_args: List[str]):
add_help=False,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog="ratings",
description="""Top ratings updates. [Source: MarketBeat]""",
description="""Top ratings updates. MarketBeat has changed the access to their data.
Now, a user needs to have 'MarketBeat All Access' to make the most out of this command.
[Source: MarketBeat]""",
)
parser.add_argument(
"-t",
@@ -33,7 +38,7 @@ def ratings_view(other_args: List[str]):
action="store",
dest="n_threshold",
type=check_positive,
default=100,
default=0, # Change to 0 because of the preview shown by MarketBeat
help="Minimum threshold in percentage change between current and target price to show ratings",
)
@@ -49,69 +54,80 @@ def ratings_view(other_args: List[str]):
ratings = marketbeat_model.get_ratings()
df_ratings = pd.DataFrame(ratings)
df_ratings = df_ratings[df_ratings["target_price"] != ""]
df_ratings["old_target"] = df_ratings["target_price"].apply(
lambda x: x.split(" ➝ ")[0] if "➝" in x else ""
df_ratings["old_target"] = (
df_ratings["target_price"]
.apply(lambda x: x.split(" ➝ ")[0].replace("$", "") if "➝" in x else "")
.astype(float)
)
df_ratings["new_target"] = df_ratings["target_price"].apply(
lambda x: x.split(" ➝ ")[1] if "➝" in x else x
df_ratings["new_target"] = (
df_ratings["target_price"]
.apply(lambda x: x.split(" ➝ ")[1].replace("$", "") if "➝" in x else x)
.astype(float)
)
df_ratings["clean_current_price"] = df_ratings["current_price"].apply(
lambda x: "".join(c if c.isdigit() or c == "." else "" for c in str(x))
)
df_ratings["clean_current_price"] = df_ratings["clean_current_price"].astype(
float
)
df_ratings["clean_target_price"] = df_ratings["new_target"].apply(
lambda x: "".join(c if c.isdigit() or c == "." else "" for c in str(x))
)
df_ratings["clean_target_price"] = df_ratings["clean_target_price"].astype(
float
df_ratings["clean_current_price"] = (
df_ratings["current_price"]
.apply(lambda x: x.replace("0.0%", "").replace("+", "").replace("$", ""))
.astype(float)
)
df_ratings["pct_increase"] = (
df_ratings.drop(
columns=["analyst", "current_price", "target_price"], inplace=True
)
df_ratings["pct_increase"] = round(
100
* (df_ratings["clean_target_price"] - df_ratings["clean_current_price"])
/ df_ratings["clean_current_price"]
* (df_ratings["new_target"] - df_ratings["clean_current_price"])
/ df_ratings["clean_current_price"],
2,
)
df_ratings["pct_increase"] = df_ratings["pct_increase"].round(2)
df_ratings = df_ratings.sort_values(by=["pct_increase"], ascending=False)
df_ratings["pct_abs"] = np.abs(df_ratings["pct_increase"])
df_ratings["pct_abs"] = df_ratings["pct_abs"].round(2)
df_ratings = df_ratings.sort_values("pct_abs", ascending=False)
df_ratings_top = df_ratings[df_ratings["pct_abs"] > ns_parser.n_threshold][
[
"ticker",
"action",
"brokerage",
"rating",
"current_price",
"rate",
"old_target",
"new_target",
"clean_current_price",
"pct_increase",
]
]
df_ratings_top["old_target"] = df_ratings_top["old_target"].apply(
lambda x: str(x) + " $"
)
df_ratings_top["new_target"] = df_ratings_top["new_target"].apply(
lambda x: str(x) + " $"
)
df_ratings_top["clean_current_price"] = df_ratings_top[
"clean_current_price"
].apply(lambda x: str(x) + " $")
df_ratings_top["pct_increase"] = df_ratings_top["pct_increase"].apply(
lambda x: str(x) + " %"
)
print(
df_ratings_top.rename(
columns={
"ticker": "Ticker",
"brokerage": "Brokerage",
"rating": "Rating",
"current_price": "Current Price",
"old_target": "Old Target",
"new_target": "New Target",
"pct_increase": "Percentage Increase",
}
).to_string(index=False)
df_ratings_top = df_ratings_top.rename(
columns={
"old_target": "Old Target",
"new_target": "New Target",
"clean_current_price": "Current Price",
"pct_increase": "Increase",
}
)
print(df_ratings_top.to_string(index=False))
print("")
except Exception as e:
print(e, "\n")

View File

@@ -72,7 +72,15 @@ def crypto_market_report(other_args: List[str]):
analysis_notebook + "." + ns_parser.mode,
)
)
print("")
print(
"Exported: ",
os.path.join(
os.path.abspath(os.path.join(".")), analysis_notebook + ".html"
),
"\n",
)
except Exception as e:
print(e, "\n")

View File

@@ -87,7 +87,15 @@ def dark_pool_report(other_args: List[str]):
analysis_notebook + "." + ns_parser.mode,
)
)
print("")
print(
"Exported: ",
os.path.join(
os.path.abspath(os.path.join(".")), analysis_notebook + ".html"
),
"\n",
)
except Exception as e:
print(e, "\n")

View File

@@ -88,7 +88,15 @@ def due_diligence_report(other_args: List[str]):
analysis_notebook + "." + ns_parser.mode,
)
)
print("")
print(
"Exported: ",
os.path.join(
os.path.abspath(os.path.join(".")), analysis_notebook + ".html"
),
"\n",
)
except Exception as e:
print(e, "\n")

View File

@@ -72,7 +72,15 @@ def economy_data_report(other_args: List[str]):
analysis_notebook + "." + ns_parser.mode,
)
)
print("")
print(
"Exported: ",
os.path.join(
os.path.abspath(os.path.join(".")), analysis_notebook + ".html"
),
"\n",
)
except Exception as e:
print(e, "\n")