well in this tutorial we are going to make script that tracks bitcoin price every hour with web scraping technique
so first we are going to use this third-party libraries, make sure to download them before we start :
- requests
- beautifulsoup4
- lxml
well the first thing we'll need to do to scrape a web page is to download the page, we can download it using requests library
i am going to use this website, you can use any website you want
now we are going to use beautifulsoup to parse this document and extract the text from the <tag> we want,
to know tha tag you want to extract the text from it you can use inspect element in Firefox or chrome
and now i want to know the Price tag in html code
and that's all i want to know now we can parse this tag from the page we downloaded before using Beautifulsoup
in this code i used lxml which is the parser we will use to parse the tag we want ! you can use html parser as well
and now if we print soup we can see the html code of the website
here's the result :
now after we have located the tag we want to parse using inspect element, we can easily parse it by this code :
so 'span' is the tag that contain the data we want ! and the class_ is the class of this tag :
and if we print price we'll get this :
After we have the tag, we can get the data by getting its text:
the output :
that all !
if we want to track bitcoin price every hour we will need to create a while loop that repeat this operation every hour:
While True means loop forever
you will need to import time library to use this code time.sleep()
and i set time.sleep(3200) which means 1 hour
so it's going to do this operation forever every hour
hope that was helpful <3