Home > Blockchain >  Getting "None" for BS4 web scraping
Getting "None" for BS4 web scraping

Time:02-20

So I am trying to create a code that will get the price of bitcoin For some reason running this code will result in the output of None, however I would like the output of the current bitcoin price, how do I fix this?

url = 'https://www.google.com/search?q=bitcoin price'
        r = requests.get(url)
        soup = BeautifulSoup(r.text, 'html.parser')

        text = soup.find('span', {'class':'vpclqee'})
        print(text)

CodePudding user response:

If you have no restriction on using Google's Bitcoin price, some other sites have easier access to this value, like CoinMarketCap:

from bs4 import BeautifulSoup
import requests

url = 'https://coinmarketcap.com/currencies/bitcoin/'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')

text = soup.find_all('div', {'class':"priceValue"})

for elem in text:
    print(elem.get_text())

But note that this is not suitable for any real-time updating as I believe it updates much too slowly.

Output:

$39,878.01

CodePudding user response:

Maybe you have an unnecessary v try to write:

text = soup.find('span', {'class':'pclqee'})

insteadof:

text = soup.find('span', {'class':'vpclqee'})

enter image description here

  • Related