BangolPhoenix
BangolPhoenix

Reputation: 393

BeautifulSoup code question regarding data type

I don't understand why it doesn't work.

Now I'm working on the financial table at:

https://finance.yahoo.com/quote/ATVI/financials?p=ATVI

What I don't get is the result of find_all method. When I put more dot notation on it like find_all('td').children It throws an error. Maybe my mistake is from the fact the return value of find_all is an object, not a list right?

And I don't have a clue as to why the code below doesn't work.

span_tag1=soup.find_all('td')
for i in span_tag1.children:
    print(i.get_text)

Upvotes: 1

Views: 692

Answers (4)

QHarr
QHarr

Reputation: 84465

I would go with pandas to get a nicely formatted table then slice out what you want

import pandas as pd

tables = pd.read_html('https://finance.yahoo.com/quote/ATVI/financials?p=ATVI')
print(tables[0].fillna(''))

Upvotes: 2

DirtyBit
DirtyBit

Reputation: 16782

Loop over the span_tag1 list to get each elem inside it:

import requests
from bs4 import BeautifulSoup

page = requests.get("https://finance.yahoo.com/quote/ATVI/financials?p=ATVI")
soup = BeautifulSoup(page.content, 'html.parser')

td = soup.find_all('td')

for et in td:
   for elem in et:
      print(elem.text)

OUTPUT:

Revenue
12/31/2018
12/31/2017
12/31/2016
12/31/2015
Total Revenue
7,500,000
7,017,000
6,608,000
4,664,000
Cost of Revenue
2,517,000
2,501,000
.
.

Upvotes: 0

Barmar
Barmar

Reputation: 781814

find_all() returns a list, so you need to loop over it. You can then use children on the elements, and call get_text() on them.

for td in soup.find_all('td'):
    for child in td.children:
        print(child.get_text())

Notice that get_text() is also a method, you need parentheses after it.

Upvotes: 0

Tjofoed
Tjofoed

Reputation: 121

Since you're finding all td elements (which creates a list), you need to loop through each one, and then find the children of each td element:

for td in soup.find_all('td'):
    for child in td.children:
        print(child.get_text())

Upvotes: 3

Related Questions