Skip to main content

Querying Live Stock Prices in Python Without an API

When I first got into Python, I had a clear goal from the very start: I wanted to make a stock ticker that I could choose a company, and it would show me the price in real time. Due to the beautiful simplicity of Python, I found myself getting closer to achieving this goal in less time than I thought I would need. Once I got the basics of Python down, I started searching for how to query a live stock price.

There are plenty of APIs that are happy to give you live stock data... for a handsome fee. Google Finance had a promising API, though it seems to have since been abandoned. Most of the decent free APIs I came across had a dishearteningly low daily query limit. In the end, I decided on a less respectable method: web scraping. 

Through much trial and error, I have simplified the code quite a bit. I have also sorted through dozens of websites to see who does and does not monitor/throttle excessive requesting. I have come to the conclusion that Yahoo Finance is best for scraping stock data. I have queried hundreds of times in a day before, and have never gotten denied a request (for any other reason than a timeout). 



Perhaps there is meaning here that I don't yet understand, but Yahoo's HTML tags for the live data seem very unintuitive. You'll see what I mean. Here is my code for finding a stock's live price:

def getprice(ticker):
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
    url = "https://finance.yahoo.com/quote/"+ticker
    page = requests.get(url, headers=headers)
    soup = BeautifulSoup(page.text, "html.parser")
    james = soup.find_all("span", {"class": "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)"})
    james = re.findall(">(.*)<", str(james))
    return james[0]
Let's break this down a little. 

 headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}

The headers aren't necessary for querying Yahoo Finance, as they won't penalize you even if they know you're querying from Python. I include them just in case. They serve to make Yahoo Finance think that my IP address is requesting their data from Mozilla Firefox running on a Mac. 

The URL is then defined simply by taking the base page URL and adding the ticker on the end of it. The ticker is passed in when querying the price. 

  url = "https://finance.yahoo.com/quote/"+ticker

I then define the variable page, which uses the requests package to create a handle to the webpage. From there, BeautifulSoup is used to parse the handle into a bs4 object that mimics webpage HTML. 

page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.text, "html.parser")

I ran out of ideas for what to name my variables, so the list that is created from using find_all to search for tags is called James. As I mentioned, Yahoo Finance's tags seem very arbitrary. In this case, the tag for the live price is "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)". If anyone can break this down, I'd be quite grateful. For now, I'm just glad it works. 

james = soup.find_all("span", {"class": "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)"})

From there, I use the re package to find all the text from the stock price tag, and snip the part of that text that lies between a greater-than (>) and a less-than (<), as the numeric value lies there. 

Since re.findall() always returns a list, even if there is only one result, I return james[0]. 

james = re.findall(">(.*)<", str(james))
return james[0]

This is the most important part of my code. In actual use, the last line is within an if statement that ensures that james[0] actually has anything, and if it doesn't, it has a couple of troubleshooting loops to try before giving up. Here's a quick video of the code in action:

 

The way to go about this for finding a stock's percent change is almost exactly the same, except the Yahoo Finance HTML class for percent change is "Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($negativeColor)". 


Comments

  1. The greatest slot machine to play is the one which comes with the right mixture of volatility, return to participant, limits, and casino bonus. The house edge, which is a truly necessary a part of} the game for players, additionally be|can be} decided by the these metrics for a web-based casino. Gambling businesses keep varieties of|these sort of|most of these} statistics beneath gloves, therefore players never truly get a crystal clear thought of the particular 바카라사이트 odds, the house edge, as well as|in addition to} payback share.

    ReplyDelete

Post a Comment

Popular posts from this blog

Apple, Inc. Passes Saudi Aramco on Track to be World's First $2T Company; Shares Overvalued

Article by Christopher Larson of Evergreen Financial Research ( @tophlars ) Shares of Apple, Inc. ( NASDAQ: $AAPL ) traded higher today, reaching a market cap of $1.90T, before rolling over and erasing most of the day's gains on Tuesday afternoon. Apple has been on fire recently (in a good way), after they reported better-than-expected sales this past quarter. One of the more memorable items on Apple's balance sheet was the company's current cash position: $192.8 billion in cash on-hand, or about $45/share of cash. On this, Kaden McBride ( @McBrideKade ), Chief Analyst at Evergreen Capital, tweeted, "I guess you can save quite a bit on R&D when you don't innovate." Apple, Inc. has 4,280,000,000 shares outstanding. To reach a record-breaking market cap of two trillion, shares of Apple would need to reach a price of $467.29 per share. Evergreen feels safe issuing a "Soft Sell" rating to Apple, or a 2/5 rating, based on current prices, and with resp

Tesla Continues Parabolic Rise; How Evergreen Will Play Options

Tesla has had a twelve month run that, if one can assume that a conservatively managed fund will average 6% YoY, equates to 190 years worth of gains. For much of this time, Evergreen Financial Research has pushed the case that shares of Tesla were extremely undervalued, and that the company was gripped by the lapels and held underwater by most mainstream financial news outlets. For the first time ever, Evergreen Financial Research believes that Tesla has, from a valuation standpoint, grown into its well-deserved shell. Tesla remains an interestingly valued firm; while Tesla seems to be overvalued by a fundamental perspective (EPS 1094), there is hardly a shred of a guarantee that the market will not continue to value Tesla based on the assumption that the firm succeeds in pioneering the transition to renewable energy, and eats every ICE automobile in the process. Tesla shattered many bears' expectations in Q2 of this year by posting a profit, and those who follow the stock know tha

AMD Continues to Grow; Poised to Steal Market Share from Intel, Nvidia

Two of AMD's highest selling consumer/enthusiast CPUs - Ryzen 9 3900X (left), Ryzen 7 3700 X (right) Shares of Advanced Micro Devices (NASDAQ: $AMD ) have torn through the $70's as if it were a blink, shooting up following the most recent earnings report. As of Thursday's market close, shares of $AMD traded at $86.71/share. Given 1170000000 shares outstanding, this gives AMD a market cap of $101.45 billion.  AMD's earnings showed a continuation of healthy reports and good news. Revenue increased 8% QoQ and 26% from 2Q2019, while operating expenses increased by 6.2% QoQ and 20.1% YoY. Gross margins gained 3pp YoY but lost 2pp QoQ, and for the past quarter were 44%.   We believe AMD to be very uniquely positioned. AMD has risen in market share over Intel for seven consecutive quarters. The last time that CPU market share was this close between two manufacturers was in 2005. While Intel Corporation's CPUs tend to perform better on a per-core/per-thread basis, AMD's