1

I am trying to get value 26.70 from this span tag:

<span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid="31">26.70</span>

I tried this:

html_text2=requests.get('https://finance.yahoo.com/quote/WRD.PA?p=WRD.PA&.tsrc=fin-srch').text
soup2 = BeautifulSoup(html_text2,'lxml')
data = soup2.select_one('span.Fz(36px)').text.strip()
print(data)

but I am getting this error:

soupsieve.util.SelectorSyntaxError: Invalid character '(' position 7
  line 1:
span.Fz(36px)
       ^
2
  • It looks like your error is in line 1 which is not included here... Commented Sep 1, 2021 at 16:03
  • Try escaping the parentheses: Fz\(36px\) Commented Sep 1, 2021 at 16:04

2 Answers 2

1

Perhaps avoid dynamic classes an look for more stable elements, and if necessary, use the relationship between those to target that node:

import requests
from bs4 import BeautifulSoup as bs

r = requests.get('https://finance.yahoo.com/quote/WRD.PA?p=WRD.PA&.tsrc=fin-srch', headers = {'User-Agent':'Mozilla/5.0'})
soup = bs(r.content, 'lxml')
print(soup.select_one('div:has(> #quote-market-notice) > span').text)
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this without BeautifulSoup if you want:

from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('https://finance.yahoo.com/quote/WRD.PA?p=WRD.PA&.tsrc=fin-srch')
element = driver.find_element_by_class_name("Fz\(36px\)")
print(element.text)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.