-1

I want to scrape yahoo! finance and get the numerical value for "Net Assets" of any security:

import requests
from bs4 import BeautifulSoup
sii = "AGG"
url = "https://finance.yahoo.com/quote/%s?p=%s&.tsrc=fin-srch" % (sii, sii)
response = requests.get(url) # send request to yahoo!
soup = BeautifulSoup(response.text, "html.parser")

When I do this:

soup.find_all("span")

It returns:

 [ <span data-reactid="53">Net Assets</span>,
   <span class="Trsdu(0.3s) " data-reactid="55">72.72B</span>,]

My question is , how do I access the numerical value inside the last tag , eg. 72.72B ?

thomas.mac
  • 1,236
  • 3
  • 17
  • 37
  • What is the issue, exactly? Have you tried anything, done any research? – AMC Feb 12 '20 at 02:27
  • Does this answer your question? [Using BeautifulSoup to extract text without tags](https://stackoverflow.com/questions/23380171/using-beautifulsoup-to-extract-text-without-tags) – AMC Feb 12 '20 at 02:28

1 Answers1

1

find_all() returns a list, so if you want the information inside the last tag, you can do this:

result = soup.find_all("span") 
myvalue = result[-1].text

If it's less about getting the last item and rather about getting the text from the span that matches a certain value, you can do the same thing with and key/value condition.

result = soup.find_all("span", {"data-reactid":"82"}) 
myvalue = result[0].text
Matt L.
  • 3,431
  • 1
  • 15
  • 28
  • thanks ! the .text is what I was looking for . I would use the data-reactid but in this case it changes between securiteis – thomas.mac Feb 11 '20 at 23:33