Tracking Bitcoin Price Using Python ? (web scraping Guide)

Tracking Bitcoin Price Using Python ? (web scraping Guide)

By crypt0z | crypto0z | 23 Mar 2020


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

python-download-page

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

inspect

and now i want to know the Price tag in html code

price-inspect

and that's all i want to know now we can parse this tag from the page we downloaded before using Beautifulsoup

python-script-2

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

python-script-3

here's the result :

html code

now after we have located the tag we want to parse using inspect element, we can easily parse it by this code :

python-script-5

so 'span' is the tag that contain the data we want ! and the class_ is the class of this tag :

python-script-7

and if we print price we'll get this :

python-output

After we have the tag, we can get the data by getting its text:

python-script-8

the output :

price

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:

final

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

 

 

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.