use of .first() to select a 'name' child element?

548 views
Skip to first unread message

W P

unread,
Mar 14, 2011, 10:16:37 AM3/14/11
to beautifulsoup
I have to parse an XML tree that frequently using a tag named 'name'.
I was wondering what the best way to select these elements is, but see
two contradicting statements in the Beautiful Soup documentation:

> Previous versions of Beautiful Soup had methods like first,
fetch, and fetchPrevious. These methods are sitll there, but they're
deprecated, and may go away soon.


> If you're looking for tag names that aren't valid Python
identifiers (like hyphenated-name), you need to use first.

I know the tag name 'name' is a valid Python identifier, but I can't
use it to select the tag because Beautiful Soup uses it as an
attribute of the element instance. I have been using .first('name')
to select it, but what should I do if .first() goes away in a future
release? I have no control over the XML schema of the document I am
parsing, it is XML that is returned from an external web service.

Leonard Richardson

unread,
Mar 14, 2011, 4:27:29 PM3/14/11
to beauti...@googlegroups.com
>    > If you're looking for tag names that aren't valid Python
> identifiers (like hyphenated-name), you need to use first.

This is a typo. (It was accurate for BS2 but was not changed for BS3.)
You should use find() instead of first().

Leonard

Reply all
Reply to author
Forward
0 new messages