2

I am making my first program with beautifulsoup and my html file has code snippet like this

......
......
......

<font face="verdana" size="3">5 n i D 1 C</font>
......
......

I want to extract 5 n i D 1 C from within the <font> tag. I am doing

ele=soup('font', face="verdana",size="3") then

ele.Contents()

but i am getting an error AttributeError: 'ResultSet' object has no attribute 'font' when i do

 print ele

output is

[<font face="verdana" size="3">5 n i D 1 C</font>]

that means it is searching correctly, please help me how to extract the given text.

Thank you

sum2000
  • 1,363
  • 5
  • 21
  • 36

2 Answers2

3
ele[0].contents[0]
u'5 n i D 1 C'
Asterisk
  • 3,534
  • 2
  • 34
  • 53
  • 1
    1. `ele=soup('font', face="verdana",size="3")`, here ele's type is a ResultSet, which seems like a list. So to get the first font element you have to get the first item in ResultSet, i.e. `ele[0]`. 2. ele[0].contents is a list of strings. So to get the first string you again access the 0-th item. – Asterisk Mar 24 '12 at 18:19
  • also, how to store just `5niD1C` in a variable? – sum2000 Mar 24 '12 at 18:46
  • `my_var = ele[0].contents[0]` – Asterisk Mar 24 '12 at 18:49
  • but this also stores newlines and tabs with my_var, how to remove them? – sum2000 Mar 24 '12 at 18:51
1

Try doing

ele[0].Contents()

instead of

ele.Contents()

ele is a ResultSet and not the first result, might want to check the documentation for something that only returns the first result. I'm not familiar with BeautifulSoup

Wessie
  • 3,460
  • 2
  • 13
  • 17