NoobLord
NoobLord

Reputation: 23

Beautiful soup not finding table ID

I am trying to extract some data from a webpage that has multiple tables. All the tables have an id="name" attribute. I am using beautiful soup 4 with Python 3.4.1. My code lopped through the first tables just fine, but on the last one it returns 'None' and I can't figure out why.

The html code for the table info is below and from what I can see, it was not formatted any differently than the other tables that had other id names such as id=Datagrid1

<TR>
<TD vAlign=top>
  <TABLE id=Datagrid7 
  style="FONT-SIZE: smaller; FONT-FAMILY: Verdana; WIDTH: 675px; BORDER-COLLAPSE: collapse" 
  cellSpacing=0 rules=all align=left border=1>
    <TBODY>

The python code below returns None, but will work if I change the id to another known id name.

table = soup.find('table', id='DataGrid7')  
print(table)

Upvotes: 0

Views: 1941

Answers (2)

sundar nataraj
sundar nataraj

Reputation: 8692

there was typo error in your program it should be small 'g'

from bs4 import BeautifulSoup

html="""<TR>
<TD vAlign=top>
  <TABLE id=Datagrid7
  style="FONT-SIZE: smaller; FONT-FAMILY: Verdana; WIDTH: 675px; BORDER-COLLAPSE: collapse"
  cellSpacing=0 rules=all align=left border=1>
    <TBODY>"""

soup=BeautifulSoup(html)


print soup.find('table',id='Datagrid7')

#output <table align="left" border="1" cellspacing="0" id="Datagrid7" rules="all" style="FONT-SIZE: smaller; FONT-FAMILY: Verdana; WIDTH: 675px; BORDER-COLLAPSE: collapse">
<tbody></tbody></table>

Upvotes: 1

falsetru
falsetru

Reputation: 369094

There's a typo in the code.

The id of the table is Datagrid7, not DataGrid7:

table = soup.find('table', id='Datagrid7')
#                                  ^

Upvotes: 1

Related Questions