Real Time Stock Price Scraping with Python and Beautiful Soup

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys this is Alice from stray called darknets today I'm going to be showing you how to get stock price updates constantly using beautifulsoup while loop and just a very simple algorithm okay so the first thing we wanted to do is to import BS for inquiry quest and from PS 4 we want to import beautifulsoup so I already have it type out here I'm just gonna run that okay and then the next thing we wanted to do is to like I show you in the last video to get the URL set are equal two requests that gets and remember you string of the page the URL if the page you want to scrape in our case this is the URL okay so next thing wants to do is to set soup equal to PS for got beautiful we're gonna do or that's hex and I'm going to do there okay so you can check your element now this is your soup okay you can also some people call it us and things this worked out fine I'm going to put it on actual script okay so the next thing you want to do is to go to this page and this is the price we wanted to get so we will right-click this element and click inspect then from there we can see all of these should work not this one probably this one this one this one should work they all work I'm gonna try this one for now okay so I'm going to do soup dot fine all at first we're defying is actually gonna work you can say div but if you do this you will get many many div elements on this page so I'm just going to show you see there's a lot of development and you do not know which one is the one you want okay so instead I'm going to do maybe more specific and say comma and I'm gonna say the class in all the div element I'm looking for the one with class equal to this my 6px pros and i just copy and paste that okay so this is exactly what we wanted right here is the full text func here not just text and HTML okay so let's see if we can narrow down a little bit you see the price is always in the spend element so you can do here define spam okay well since we did that fine all earlier this is actually a list of many elements so you can either do defined or you can do fine oh and let's just say you find two element have the same class name you can say okay the first element and I won't do that so it gives you the same results here you can choose but in the case of there are multiple elements that you ended up finding you can use the list method to go to the specific one you wanted anyways let's just go for this okay so if you look closely here's your price here's the price you wanted and also if you pay attention this had already changed it's 160 6.33 one and this is 160 6.23 so we're getting an older price but that's okay I'll show you how to get the update later so things you only want the price we can just do that text and it will give us the price here we shall now we are done with the page here I'm gonna create a loop that use what we film to get the most current price to do that I would say I could set this up as a function and I can say this is death parse price and I would say to get price equals to this so if you can now see what I did is I would use request to get that page i would define soup like we did earlier in the console and I will call price as this right and in the very end you know you can't add a print statement or whatever okay let's just do return price okay okay and now we're going to start doing our loop for while true you can say print stream so when I do this I call for the function and whatever it is return will be turning to a string and come out in the print statement so I'm just gonna show you what it looks like I'm gonna run just this line without a lot oh I did not save this okay so see they will just print up every single time I run it so you can see I'm getting the most current price now every single time the price would change now we don't want it to be doing it well we could do as fast as possible so if you won't run the scrip so a little explanation here while true means just always I'm going to print this statement here this string this string is consists of the first part which is just a string says the current price and the second part is a string here it's the string of whatever parse price returns to me so right here I'm calling the function pause price and when I do that I go into the function I have created I set requests I said soup and I define what prices and finally I return the price so this price interns are returned into this string and this string come by with the first part of the string will be what prints out and then so again the while loop is evoking this function every time constantly so you're always getting the update so as you can see the same way we do get the prices with this particular stock you can also use it to follow many other stocks so you can get multiple prices and from there you can do analysis you have the script notify you when there's 10% increase for example or decrease or you can be doing a lot of manipulation with it so this is kind of a basic starting point from how to get the data for the stock price thank you guys for watching don't forget to Like and subscribe then don't be do something [Music]
Info
Channel: straight_code
Views: 128,614
Rating: 4.8817668 out of 5
Keywords: web scraping, parsing, beautifulSoup4, beautifulsoup, python, toturial, webcrawler, scraper, requests, algo trading, algotrading, stock trading
Id: rONhdonaWUo
Channel Id: undefined
Length: 10min 47sec (647 seconds)
Published: Wed Mar 27 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.