Search code examples
pythoncoinmarketcap

Coinmarketcap webscraping BCoin price


I am trying to get the price of a coinmarketcap cryptocurrency. Unfortunately it's not working. Can anyone help?

I would like to display the price of this coin: https://coinmarketcap.com/currencies/bombcrypto/

My code:

#!/usr/bin/env python3
from bs4 import BeautifulSoup as S
import requests

c = input('bombcrypto')
url = f'https://coinmarketcap.com/currencies/{c}/'

headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}

r = requests.get(url,headers=headers)
soup = S(r.content,'html.parser')
print(f'the price of {c} now is ')
x = soup.find(class_='sc-16r8icm-0 kjciSH priceTitle').text
print(x)

thank you


Solution

  • There is a couple issues with your code. Make sure you follow the structure of the webpage correctly when finding a path through the elements. I also changed the input statement on line 5. The element

    #!/usr/bin/env python3
    from bs4 import BeautifulSoup as S
    import requests
    
    c = input("Crypto: ")#'bombcrypto'
    url = f'https://coinmarketcap.com/currencies/{c}/'
    
    headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
    
    r = requests.get(url,headers=headers)
    soup = S(r.content,'html.parser')
    print(f'the price of {c} now is ')
    x = soup.find(class_='sc-16r8icm-0 kjciSH priceTitle').findChild(class_="priceValue").findChild('span')
    print(x.text)