3
                                <div id="findet_1" name="findet_1" >

                                    <table width="100%" border="0" cellspacing="0" cellpadding="0">

                                        <tr>

                                            <td class="thc01 w160 gL_10 UC" >&nbsp;Standalone</td>

                                            <td class="thc01 w160 gL_10 tar">Jun'16</td>

                                            <td class="thc01 w160 gL_10 tar">Mar'16</td>

                                            <td class="thc01 w160 gL_10 tar">Dec'15</td>

                                            <td class="thc01 w160 gL_10 tar"><div class="PR20">Sep'15</div></td>

                                        </tr>

                                        <tr>

                                            <td class="thc02 w160 gD_12" >Net Sales</td>

                                            <td class="thc02 w160 gD_12 tar">16,339.70</td>

                                            <td class="thc02 w160 gD_12 tar">15,589.40</td>

                                            <td class="thc02 w160 gD_12 tar">15,065.00</td>

                                            <td class="thc02 w160 gD_12 tar"><span class="PR20">14,824.50</span></td>

                                        </tr>

                                        <tr>

                                            <td class="thc02 w160 gD_12" >Other Income</td>

                                            <td class="thc02 w160 gD_12 tar">50.10</td>

                                            <td class="thc02 w160 gD_12 tar">46.30</td>

                                            <td class="thc02 w160 gD_12 tar">153.30</td>

                                            <td class="thc02 w160 gD_12 tar"><span class="PR20">1,087.40</span></td>

                                        </tr>

                                        <tr>

                                            <td class="thc02 w160 gD_12" >PBDIT</td>

                                            <td class="thc02 w160 gD_12 tar">6,612.30</td>

                                            <td class="thc02 w160 gD_12 tar">5,930.60</td>

                                            <td class="thc02 w160 gD_12 tar">5,543.30</td>

                                            <td class="thc02 w160 gD_12 tar"><span class="PR20">5,416.80</span></td>

                                        </tr>

                                        <tr>

                                            <td class="thc02 w160 gD_12" >Net Profit</td>

                                            <td class="thc02 w160 gD_12 tar">1,427.50</td>

                                            <td class="thc02 w160 gD_12 tar">1,693.90</td>

                                            <td class="thc02 w160 gD_12 tar">1,709.10</td>

                                            <td class="thc02 w160 gD_12 tar"><span class="PR20">2,223.70</span></td>

                                        </tr>

                                    </table>

                                </div>

I am trying to read this table. but unable to do so. I am using beautyfulsoup findall to find div first. table is present inside div. I am unable to find that table. also second question is around what is best way to traverse through rows. Here fore example I want output in csv format which should be enclosed by double quotes like : "STANDALONE","Jun'16","Mar'16","Dec'15","Sep'15" "Net Sales","16,339.70","15,589.40","15,065.00","14,824.50" "Other Income","50.10","46.30","153.30","1,087.40" "PBDIT","6,612.30","5,930.60","5,543.30","5,416.80" "Net Profit","1,427.50","1,693.90","1,709.10","2,223.70"

my code :

from urllib.request import urlopen from bs4 import BeautifulSoup import re

html = urlopen("http://www.moneycontrol.com/india/stockpricequote/computers-software/tataconsultancyservices/TCS")
bsObj = BeautifulSoup(html, "html.parser")
link = bsObj.findAll("div", id="findet_1")
table1 = link.find('table').find_all('tr')

I know we can get values using get_text and traverse through rows using for loop. but I am unable to find table only :(

Bhavesh Ghodasara
  • 1,981
  • 2
  • 15
  • 29

2 Answers2

3

Try this:

table_div = html.find('div' , {'id': 'findet_1', 'name': 'findet_1' })
table = table_div.find('table')

or this

table_div = html.find('div' , {'id': 'findet_1', 'name': 'findet_1' })
table = table_div.find_all('tr')
arcegk
  • 1,480
  • 12
  • 15
-1

The only difference is that find_all() returns a list containing the single result, and find() just returns the result.

If find_all() can’t find anything, it returns an empty list. If find() can’t find anything, it returns None:

link = bsObj.findAll("div", id="findet_1")
if link:
    table1 = link[0].find('table').find_all('tr')
kiviak
  • 1,083
  • 9
  • 10