wolfenstein11x
wolfenstein11x

Reputation: 19

Which CSS selectors to use to pull numbers from a webpage using .select() in BeautifulSoup?

I am trying to pull the current stock price from a webpage using python. I am having trouble using Beautiful Soup to pull the numbers. I don't know what CSS selector to use. I've tried combinations of span, div, tr, tbody, td. Attached is a picture of the HTML. Note that the number I am after is 368.45:

HTML:

HTML

Here is the essence of my python3 code (I am using Ubuntu in case that matters):

res = requests.get(#webpage_url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, "lxml")
stock_price = soup.select(#what CSS selector do I use?)
print(stock_price[0].getText()) 

I am basically just asking how do I use the .select() method to get the highlighted text in the attached HTML snapshot.

Upvotes: 1

Views: 82

Answers (1)

Hans Lehnert
Hans Lehnert

Reputation: 474

You could try matching the cell with the posquote class that contains the span and get the string from there

stock_price = soup.find('td', class_='posquote').string

Upvotes: 1

Related Questions