Home > database >  How to filter on this artifact in the HTML?
How to filter on this artifact in the HTML?

Time:01-03

I am using this code and it works:

from bs4 import BeautifulSoup
import sys
import requests
page = requests.get("https://finance.yahoo.com/quote/GOOGL?p=GOOGL")
soup = BeautifulSoup(page.content, 'html.parser')
fin-streamer= soup.find("fin-streamer", class_="Fz(36px)")
print(fin-streamer)
print (fin-streamer.get_text())

It prints this for fin-streamer:

<fin-streamer active=""  data-field="regularMarketPrice" data-pricehint="2" data-reactid="47" data-symbol="GOOGL" data-test="qsp-price" data-trend="none" value="2897.04">2,897.04</fin-streamer>

What I'd like to do is filter on something more useful than the Fz(36px) class, such as

data-symbol="GOOGL"

but I don't know the syntax for that.

CodePudding user response:

You can use a dictionary:

fin_streamer = soup.find("fin-streamer", {"data-symbol":"GOOGL"})

CodePudding user response:

How to select?

To select your element more specific, simply take use of css selectors:

soup.select_one('fin-streamer[data-symbol="GOOGL"]')

Above line selects the first <fin-streamer> with attribute data-symbol="GOOGL" - To get its value just call ['value] as alternativ call .text method.

Note: There is a difference in format of value / text

Example

from bs4 import BeautifulSoup
import requests
page = requests.get("https://finance.yahoo.com/quote/GOOGL?p=GOOGL")
soup = BeautifulSoup(page.content, 'html.parser')
soup.select_one('fin-streamer[data-symbol="GOOGL"]')['value']
  • Related