Home > Net >  Trying to get the string inside of <div> tags using bs4 (python3)
Trying to get the string inside of <div> tags using bs4 (python3)

Time:07-02

Please be patient with me. Brand new to Python and Stackoverflow.

I am trying to pull crypto price data into a program in order find out exactly how much I have in usd. I am currently stuck trying to extract the string from the tag that I get back. What I have so far:

It won't allow me to add a picture of my post yet so here is a link: [1]: https://i.stack.imgur.com/DVlxe.png

I will also put the code on here, Please forgive the formatting.

from bs4 import BeautifulSoup
import requests

url = ('https://coinmarketcap.com/currencies/shiba-inu/')
response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')

price = soup.find_all("div", {"class": "imn55z-0 hCqbVS price"})


for i in price:
    prices = (i.find("div"))

print(prices)

I am wanting to pull the string out to turn it into an int to do some math equations on later in the program.

Any and all help will be much appreciated.

CodePudding user response:

You don't need the whole class (which possibly might change), it should just work with price. Try the following:

from bs4 import BeautifulSoup
import requests

url = 'https://coinmarketcap.com/currencies/shiba-inu/'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
div_price = soup.find('div', class_="price")
price = div_price.div.text

print(price)

This displayed:

$0.00001019
  • Related