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/^TNX?p=^TNX')
# 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 data-reactid="40" data-test="PREV_CLOSE-value">1.4340</td>
CodePudding user response:
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)
CodePudding user response:
Your code is working fine. You need to inject user-agent and calls text method to get string as value as follows:
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'}
# Making a GET request
r = requests.get('https://finance.yahoo.com/quote/^TNX?p=^TNX',headers=headers)
# Parsing the HTML
soup = BeautifulSoup(r.content, 'lxml')
# print(soup.prettify())
match = soup.find('td', class_='Ta(end) Fw(600) Lh(14px)')
print(match.text)
# <td data-reactid="40" data-test="PREV_CLOSE-value">1.4340</td>
Output
1.4340
CodePudding user response:
Answer
thing = str('<td 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.