Lucas Kauffman
Lucas Kauffman

Reputation: 6891

Beautiful Soup line matching

Im trying to build a html table that only contains the table header and the row that is relevant to me. The site I'm using is http://wolk.vlan77.be/~gerben.

I'm trying to get the the table header and my the table entry so I do not have to look each time for my own name.

What I want to do :

What I am doing now :

I want to get the line from the html file that contains lucas, however when I run it like this I get this in my output :

 **************** 


<tr><td>lucas.vlan77.be</td> <td><span style="color:green;font-weight:bold">V</span></td> <td><span style="color:green;font-weight:bold">V</span></td> <td><span style="color:green;font-weight:bold">V</span></td> </tr>



false

Now I don't get why it doesn't match, the string lucas is clearly in there :/ ?

Upvotes: 0

Views: 2339

Answers (2)

johnsyweb
johnsyweb

Reputation: 141878

It looks like you're over-complicating this.

Here's a simpler version...

>>> import BeautifulSoup
>>> import urllib2
>>> html = urllib2.urlopen('http://wolk.vlan77.be/~gerben')
>>> soup = BeautifulSoup.BeautifulSoup(html)
>>> print soup.find('td', text=lambda data: data.string and 'lucas' in data.string)
lucas.vlan77.be

Upvotes: 3

Mikhail Polykovskii
Mikhail Polykovskii

Reputation: 2393

It's because line is not a string, but BeautifulSoup.Tag instance. Try to get td value instead:

if '''lucas''' in line.td.string:

Upvotes: 1

Related Questions