NeoTheNerd
NeoTheNerd

Reputation: 656

Python - BeautifulSoup - Parse Value

I'd like to parse a value from a webpage using beautiful soup. I've found the value using my script. I'm just seeking help to parse the value '1.4340' from the match.

import requests
from bs4 import BeautifulSoup

# Making a GET request
r = requests.get('https://finance.yahoo.com/quote/%5ETNX?p=%5ETNX')

# Parsing the HTML
soup = BeautifulSoup(r.content, 'lxml')
#print(soup.prettify())

match = soup.find('td', class_='Ta(end) Fw(600) Lh(14px)')
print(match)
#<td class="Ta(end) Fw(600) Lh(14px)" data-reactid="40" data-test="PREV_CLOSE-value">1.4340</td>

BeautifulSoup

Upvotes: 1

Views: 146

Answers (2)

ConfirmedSavage
ConfirmedSavage

Reputation: 11

Answer

thing = str('<td class="Ta(end) Fw(600) Lh(14px)" data-reactid="40" data-test="PREV_CLOSE-value">1.4340</td>')
begin = False
newStr = ''

for item in thing:
    if item is '<':
        begin = False
    if begin is True:
        newStr += item
    if item is '>':
        begin = True

print(float(newStr))

Output

1.434

We can just use the syntax of html to guide us to simply make a new string and then convert it to a float. You can keep it as a string if you'd like.

Upvotes: -1

user17228732
user17228732

Reputation:

You need to get the text from the tag.

match = soup.find('td', class_='Ta(end) Fw(600) Lh(14px)')
text = match.getText()
print(text)

Upvotes: 3

Related Questions