Search code examples
beautifulsoupyahoo-finance

how do I parse info from yahoo finance with beautiful soup


I have got so far by using soup.findAll('span')

<span data-reactid="12">Previous Close</span>,
     <span class="Trsdu(0.3s) " data-reactid="14">5.52</span>,
     <span data-reactid="17"></span>,
     <span class="Trsdu(0.3s) " data-reactid="19">5.49</span>,
     <span data-reactid="38">Volume</span>,
     <span class="Trsdu(0.3s) " data-reactid="40">1,164,604</span>,
     ...

I want a tabkle that shows me

Open 5.49
Volume 1,164,604

... I tried soup.findAll('span').text but it gives error msg:

ResultSet object has no attribute 'text'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

this is the source:

https://finance.yahoo.com/quote/gxl.ax?p=gxl.ax

Solution

  • Luckily the error gives us a hint:

    You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

    Try one of these:

    soup.findAll('span')[0].text
    soup.findAll('span')[i].text
    soup.find('span').text
    

    This is a generic problem when navigating many selector systems, CSS selectors included. To operate on an element it must be a single element rather than a set. findAll() returns a set (array), so you can either index into that array (e.g. [i]) or find the first match with find().