0

I have problem similar to describe here, but a bit more complicated. There are BeautifulSoup objects (store in list) and I want to find some other tags. The information which tags I want to find are store in strings. I.e.:

a= [...] #(list of BeautifulSoup objects)
next="findNext('span')"

b=[ getattr(c,next).string for c in a]

doesn't work. What I do wrong.

Community
  • 1
  • 1
Wawrzek
  • 452
  • 5
  • 18
  • 2
    Are you getting an error message or incorrect output? Can you post an example? "Doesn't work" is not very helpful description of your problem. – Adeel Zafar Soomro Mar 25 '11 at 01:45
  • next='findNext("span")' compare a1 = [ getattr(c, next) for c in a.atags] to a2 = [ c.findNext("span") for c in a.atags] a1[0] = None when a2[0] = Linguamatics So it works indeed, just not as expected. – Wawrzek Mar 26 '11 at 00:21

2 Answers2

1

Looks to me like what you want is:

b = [ eval("c." + next).string for c in a ]

This will call findNext('span') for each element c of the list a and form a list of the results of each findNext call in the list b.

srgerg
  • 18,719
  • 4
  • 57
  • 39
0

Try

trees = [...] #(list of BeautifulSoup objects)
strings = [tree.findNext('span').string for tree in trees]

or, if you really must,

trees = [...] #(list of BeautifulSoup objects)
next = ('findNext', ('span',))
strings = [getattr(tree, next[0])(*(next[1])).string for tree in trees]

So I guess the next question is, what's a simple way to turn "findNext('span')" into ('findNext', ('span',)) (keeping in mind that there may be multiple arguments)?

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • The problems is that "findNext('span')" is a example and it will change from page to page. I plan to read it from a database (or a dictionary). I guess it would address your concern, doesn't it? – Wawrzek Mar 26 '11 at 00:38