Need help with this code

Joined
Mar 31, 2023
Messages
95
Reaction score
8
Hello everyone,

I am currently facing a minor issue that I am unable to resolve. The problem is that my code is returning values that seem unrelated and I am unsure of the reason behind it.

I would greatly appreciate any help to resolve this matter.
Here's the code:

Python:
import requests
from bs4 import BeautifulSoup

com_list = ['AAPL']
symbol = com_list[0]
url = f'https://finance.yahoo.com/quote/{symbol}?p={symbol}&.tsrc=fin-srch'

response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

name = soup.find('h1', {'class': 'D(ib) Fz(18px)'}).text
print(name)

regularMarketPrice = soup.find('fin-streamer', {'class': 'Fw(b) Fz(36px) Mb(-4px) D(ib)','data-symbol':symbol,'data-test':'qsp-price','data-pricehint': '2', 'data-field': 'regularMarketPrice'})['value']
regularMarketPrice_ = float(regularMarketPrice)

regularMarketChange = soup.find('fin-streamer', {'class': 'Fw(500) Pstart(8px) Fz(24px)','data-symbol':symbol,'data-test':'qsp-price-change','data-field': 'regularMarketChange','data-trend':'txt','data-pricehint':'2'})['value']
regularMarketChange_ = float(regularMarketChange)

regularMarketChangePercent = soup.find('fin-streamer', {'class': 'Fw(500) Pstart(8px) Fz(24px)', 'data-field': 'regularMarketChangePercent'})['value']
regularMarketChangePercent_ = str(float(regularMarketChangePercent) * 100) + '%'

print(regularMarketPrice_, ':', str(regularMarketChange_) + '%', ':', regularMarketChangePercent_)

Phro0244.
 
Last edited:
Joined
May 23, 2023
Messages
1
Reaction score
0
did this helps you out?



import requests
from bs4 import BeautifulSoup

com_list = ['AAPL']
symbol = com_list[0]
url = f'https://finance.yahoo.com/quote/{symbol}?p={symbol}&.tsrc=fin-srch'

response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

name = soup.find('h1', {'class': 'D(ib) Fz(18px)'}).text
print(name)

regularMarketPrice = soup.find('span', {'class': 'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'}).text
regularMarketPrice_ = float(regularMarketPrice.replace(',', ''))

regularMarketChange = soup.find('span', {'class': 'Trsdu(0.3s) Fw(500) Pstart(8px) Fz(24px)'}).text
regularMarketChange_ = float(regularMarketChange)

regularMarketChangePercent = soup.find('span', {'class': 'Trsdu(0.3s) Fw(500) Pstart(8px) Fz(24px)'}).text
regularMarketChangePercent_ = str(float(regularMarketChangePercent.replace('%', '')) / 100)

print(regularMarketPrice_, ':', str(regularMarketChange_) + '%', ':', regularMarketChangePercent_)



i´m looking forward, to hear from you. :)
 
Joined
Mar 31, 2023
Messages
95
Reaction score
8
Thank you for the response!

Could you please provide information about the origin of the variable "Span"? Additionally, I'm encountering errors when running the code. Have you had a chance to review it? I would greatly appreciate any further assistance or response you can provide. Thank you once again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Need help with this script 4
Need help fixing code 1

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top