List Info

Thread: BeautifulSoup - getting cells without new line characters




BeautifulSoup - getting cells without new line characters
user name
2006-03-31 15:25:21
 From a table, I want to get the cells for then only choose
some of them. 

<table>
<tr>
<td>WY</td>
<td>Wyo.</td>
</tr>
...
</table> 

Using: 

for row in table('tr'): print row.contents 

   ['\n', <td>WY</td>, '\n',
<td>Wyo.</td>, '\n']
   [...] 

I get a new line character between each cell. 

Is possible get them without those '\n'? 

Thanks in advance! 
_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor
BeautifulSoup - getting cells without new line characters
user name
2006-03-31 15:43:52
jonasmgsofthome.net wrote:
>  From a table, I want to get the cells for then only
choose some of them. 
> 
> <table>
> <tr>
> <td>WY</td>
> <td>Wyo.</td>
> </tr>
> ...
> </table> 
> 
> Using: 
> 
> for row in table('tr'): print row.contents 
> 
>    ['\n', <td>WY</td>, '\n',
<td>Wyo.</td>, '\n']
>    [...] 
> 
> I get a new line character between each cell. 
> 
> Is possible get them without those '\n'? 

Well, the newlines are in your data, so you need to strip
them or ignore 
them somewhere.

You don't say what you are actually trying to do, maybe
this is close:
   for row in table('tr'):
     cellText = [cell.string for cell in row('td')]
     print ' '.join(cellText)

Kent

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor
BeautifulSoup - getting cells without new line characters
user name
2006-03-31 16:01:15
Kent Johnson writes: 

> jonasmgsofthome.net wrote:
>>  From a table, I want to get the cells for then
only choose some of them.  
>> 
>> <table>
>> <tr>
>> <td>WY</td>
>> <td>Wyo.</td>
>> </tr>
>> ...
>> </table>  
>> 
>> Using:  
>> 
>> for row in table('tr'): print row.contents  
>> 
>>    ['\n', <td>WY</td>, '\n',
<td>Wyo.</td>, '\n']
>>    [...]  
>> 
>> I get a new line character between each cell.  
>> 
>> Is possible get them without those '\n'? 
> 
> Well, the newlines are in your data, so you need to
strip them or ignore 
> them somewhere. 
> 
> You don't say what you are actually trying to do,
maybe this is close:
>    for row in table('tr'):
>      cellText = [cell.string for cell in row('td')]
>      print ' '.join(cellText) 
> 
> Kent 
> 
> _______________________________________________
> Tutor maillist  -  Tutorpython.org
> http://
mail.python.org/mailman/listinfo/tutor

I want only (for each row) to get some positions (i.e. 
row.contents[0],row.contents[2]) 
_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )