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)".
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