O. Barinov
O. Barinov

Reputation: 171

Find HTML table rows containing input value

Idea is to build-up a filter which would return HTML rows containing filter input value.

I came up with this so far but it returns nothing:

#!/usr/bin/python

from bs4 import BeautifulSoup
import cgi
import re
import urllib

data = cgi.FieldStorage()
filter = data.getvalue('request')

tree = urllib.urlopen('myfile.html').read()

soup = BeautifulSoup(tree)
tables = soup.findChildren('table')

mytable = tables[0]

rows = mytable.findAll('tr')
userrows = [t for t in rows if t.findAll(text=filter)]

text_file = open("tst.txt", "aw")
text_file.write(userrows)
text_file.close()

Tried with userrows = mytable.find('tr', text=filter) but same outcome. Cmd says "expected a character buffer object"

What could be a possible issue here?

PC. I tried converting text=str(filter) but same outcome

Upvotes: 0

Views: 206

Answers (1)

宏杰李
宏杰李

Reputation: 12168

there a five kind of filter that beautifulsoup sport:

  1. A string
  2. A regular expression
  3. A list
  4. True
  5. A function

make sure your filter is one of those.

Upvotes: 1

Related Questions