How to scrape within an html table?

Viewed 90

I want to scrape yahoo finance for text data. For any ticker I wish to be able to scrape the Total Revenue:

introducir la descripción de la imagen aquí

For the previous example see the L'Oreal page. The associated target code is:

                         <div class="rw-expnded" data-reactid="44" data-test="fin-row">
                            <div class="D(tbr) fi-row Bgc($hoverBgColor):h" data-reactid="45">
                               <div class="D(tbc) Ta(start) Pend(15px)--mv2 Pend(10px) Bxz(bb) Py(8px) Bdends(s) Bdbs(s) Bdstarts(s) Bdstartw(1px) Bdbw(1px) Bdendw(1px) Bdc($seperatorColor) Pos(st) Start(0) Bgc($lv2BgColor) fi-row:h_Bgc($hoverBgColor) Pstart(15px)--mv2 Pstart(10px)" data-reactid="46">
                                  <div class="D(ib) Va(m) Ell Mt(-3px) W(215px)--mv2 W(200px)" data-reactid="47" title="Total Revenue"><span class="Va(m)" data-reactid="48">Total Revenue</span></div>
                                  <div class="W(3px) Pos(a) Start(100%) T(0) H(100%) Bg($pfColumnFakeShadowGradient) Pe(n) Pend(5px)" data-reactid="49"></div>
                               </div>
                               <div class="D(tbc) Ta(c) Pstart(6px) Pend(4px) Bxz(bb) Py(8px) BdB Bdc($seperatorColor) Miw(100px) Miw(156px)--pnclg Bgc($lv1BgColor) fi-row:h_Bgc($hoverBgColor)" data-reactid="50" data-test="fin-col"><span data-reactid="51">29,873,600</span></div>
                               <div class="D(tbc) Ta(c) Pstart(6px) Pend(4px) Bxz(bb) Py(8px) BdB Bdc($seperatorColor) Miw(100px) Miw(156px)--pnclg" data-reactid="52" data-test="fin-col"><span data-reactid="53">29,873,600</span></div>
                               <div class="D(tbc) Ta(c) Pstart(6px) Pend(4px) Bxz(bb) Py(8px) BdB Bdc($seperatorColor) Miw(100px) Miw(156px)--pnclg Bgc($lv1BgColor) fi-row:h_Bgc($hoverBgColor)" data-reactid="54" data-test="fin-col"><span data-reactid="55">26,937,400</span></div>
                               <div class="D(tbc) Ta(c) Pstart(6px) Pend(4px) Bxz(bb) Py(8px) BdB Bdc($seperatorColor) Miw(100px) Miw(156px)--pnclg" data-reactid="56" data-test="fin-col"><span data-reactid="57">26,023,700</span></div>
                               <div class="D(tbc) Ta(c) Pstart(6px) Pend(4px) Bxz(bb) Py(8px) BdB Bdc($seperatorColor) Miw(100px) Miw(156px)--pnclg Pend(10px) Bgc($lv1BgColor) fi-row:h_Bgc($hoverBgColor)" data-reactid="58" data-test="fin-col"><span data-reactid="59">25,837,100</span></div>
                            </div>
                            <div class="D(b)" data-reactid="60"></div>
                         </div>

To try and get the total revenue I did this:

#This will not run on online IDE 
import requests 
from bs4 import BeautifulSoup 

ticker = 'OR.SA'
URL = "https://finance.yahoo.com/quote/OR.PA/financials?p={ticker}"
r = requests.get(URL) 

soup = BeautifulSoup(r.content, 'html5lib') 

Then I try to get the item after the Total Revenue:

soup.find('span', string='Total Revenue').find_next().text

But it returns ''

If there is a simpler way, like if there exist an API for that, don't hesitate to let me know. I tried yfinance but it didn't worked for the financial statements for non US stocks.

2 Answers

You are just getting the next div. You have to get the next span. find_next takes the name of the tag as a parameter.

Use this:

soup.find('span', string='Total Revenue').find_next('span').text

Code

import requests
from bs4 import BeautifulSoup

ticker = 'OR.SA'
URL = "https://finance.yahoo.com/quote/OR.PA/financials?p={ticker}"
r = requests.get(URL)

soup = BeautifulSoup(r.content, 'html5lib')
# soup.find('span', string='Total Revenue').find_next()
# <div class="W(3px) ...data-reactid="49"></div>
print(soup.find('span', string='Total Revenue').find_next('span').text)

Output

29,873,600

You were not specifying the right tag to find next .

#This will not run on online IDE 
import requests 
from bs4 import BeautifulSoup 

ticker = 'OR.SA'
URL = "https://finance.yahoo.com/quote/OR.PA/financials?p={ticker}"
r = requests.get(URL) 

soup = BeautifulSoup(r.content, 'lxml')

total_rev_tag = soup.find('div', {'title':"Total Revenue"}).parent.next_sibling
total_rev_txt_1 = total_rev_tag.find('span').text
total_rev_txt_2 = total_rev_tag.find_next().text
print('total_rev_txt_1',total_rev_txt_1)
print('total_rev_txt_2',total_rev_txt_2)

total_rev_txt_3 = soup.find('span', string='Total Revenue').find_next('span').text
print('total_rev_txt_3',total_rev_txt_3)

Output:

total_rev_txt_1 29,873,600
total_rev_txt_2 29,873,600
total_rev_txt_3 29,873,600
Related