Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hotfix] Finviz company_details DOM fix #204

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions finviz/main_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ def get_stock(ticker):
get_page(ticker)
page_parsed = STOCK_PAGE[ticker]

title = page_parsed.cssselect('table[class="fullview-title"]')[0]
keys = ["Ticker","Company", "Sector", "Industry", "Country"]
fields = [f.text_content() for f in title.cssselect('a[class="tab-link"]')]
data = dict(zip(keys, fields))

company_link = title.cssselect('a[class="tab-link"]')[0].attrib["href"]
title = page_parsed.cssselect('div[class="fv-container py-2.5"]')[0]
data = {}
data["Ticker"] = title.cssselect('h1[class="js-recent-quote-ticker quote-header_ticker-wrapper_ticker"]')[0].text_content().strip()
try:
company_details = title.cssselect('h2[class="quote-header_ticker-wrapper_company text-xl"]')[0]
except IndexError:
company_details = title.cssselect('h2[class="quote-header_ticker-wrapper_company"]')[0]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this try except is because there are still some tickers that don't have the text-xl class on them e.g., https://finviz.com/quote.ashx?t=SQM

data["Company"] = company_details.text_content().strip()
company_link = company_details.cssselect('a[class="tab-link block truncate"]')[0].attrib["href"]
data["Website"] = company_link if company_link.startswith("http") else None
keys = ["Sector", "Industry", "Country", "Exchange"]
fields = [f.text_content() for f in title.cssselect('a[class="tab-link"]')]
data.update(dict(zip(keys, fields)))

all_rows = [
row.xpath("td//text()")
Expand Down