Reputation: 25
I am self-learning on python and currently trying to get a crypto recent price from Binance (https://www.binance.com/en/trade/LTC_USDT). the Html of the price is shown in the following figure:image
the Html code shown in the image above is located here on the website:code location
as you can see the price of LTC (currently around $43) is mentioned in two places in the Html code.
The 1st location has its class address changes between 3 address depending on the text color based on the price movement as follows:
Red text (price down)>>>>> class="sc-1p4en3j-3 sc-1p4en3j-5 jFILqo"
Green text (price up)>>>>> class="sc-1p4en3j-3 sc-1p4en3j-4 kZlsgN"
white text (no cgange)>>>> class="sc-1p4en3j-3 sc-1p4en3j-6 czKdcJ"
The 2nd price location has its class address fixed, but it contains the $ sign.
I want to get the stock price and store so I could do math with later. I tried using both text locations to get the price. my code:
url= "https://www.binance.com/en/trade/LTC_USDT"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
tried to get the price from all classes in location 1:
newprice= soup.find('span', class_ = 'sc-1p4en3j-3 sc-1p4en3j-5 jFILqo')
newprice1= soup.find('span', class_ = 'sc-1p4en3j-3 sc-1p4en3j-4 kZlsgN')
newprice2= soup.find('span', class_ = 'sc-1p4en3j-3 sc-1p4en3j-6 czKdcJ')
print (str(newprice))
print (str(newprice1))
print (str(newprice2))
if I add .text
to the 1st three lines above. it tells me: 'NoneType' object has no attribute 'text'
tried to get it from location 2:
newprice3= soup.find('span', class_ = 'sc-1p4en3j-7 eHDQUL')
print (str(newprice3))
output:
None
None
None
None
Upvotes: 2
Views: 400
Reputation: 99011
You cannot use BeautifulSoup to retrieve the price directly from the website because it's being generated using javascript and updated via web-sockets, you should use the free Binance API instead.
Here's an example to retrieve the LTCUSDT
price:
import requests
url = "https://api.binance.com/api/v3/ticker/price?symbol=LTCUSDT"
resp = requests.get(url).json()
price = resp['price']
# 43.45000000
Upvotes: 3