تامر
unread,Oct 28, 2009, 9:35:15 AM10/28/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to beautifulsoup
When a search is run (findAll) on a match, beautiful soup will
continue and search the matched item's children. Is it possible to
prevent this behavior?
I couldn't figure it out, so I wrote the following code which works
(although it is slow):
while True:
divTag = soup.find('div', id=re.compile("post_message"))
if divTag != None:
threadContentList.append( deepcopy(divTag) )
divTag.decompose()
else:
break
Originally, I thought the recursive flag is what I needed to use.
However if recursive is set to false, the function will not search
beyond the first tag. If it is set to true, the function will search
all of the text:
soup = BeautifulSoup(<table><tr><td><table><tr><td></td></tr></table></
td></tr></table>)
# These first two examples seem to show us that "recursive" provides
us with the correct results
soup.findall("table, recursive=True)
>>[<table><tr><td><table><tr><td></td></tr></table></td></tr></table>, <table><tr><td></td></tr></table>, ]
soup.findall("table", recursive=False)
>> [<table><tr><td><table><tr><td></td></tr></table></td></tr></table>, ]
# However, searching something that is not the first element exposes
the issue
soup.findall("tr", recursive=True)
>>[<tr><td><table><tr><td></td></tr></table></td></tr>, <tr><td></td></tr>, ]
soup.findall("tr", recursive=False)
>>[ ]
Any thoughts?
Tim