AttributeError: "'NoneType' object has no attribute 'string'"

3,249 views
Skip to first unread message

Christopher Brewster

unread,
Aug 8, 2010, 6:51:33 AM8/8/10
to beautifulsoup, Christopher Brewster
How do I test in Beautifulsoup if there is a string attribute, and if there is not do some further processing, and if there is get the string?
I am always falling over this AttributeError.

Thanks,
Christopher

Aaron DeVore

unread,
Aug 8, 2010, 4:05:30 PM8/8/10
to beauti...@googlegroups.com
Christopher,
You're trying to manipulate the result of a function that returned
None. That usually happens if find and friends (not findAll*) don't
find anything (which makes them return None). To check whether find
returned something, do something like this:

result = tag.find('tag-name')
if result:
...

Or if you want to do error handling only when you haven't found anything:

if not result:
...

Or if you're feeling lazy:

assert result

Good luck,
Aaron DeVore

On Sun, Aug 8, 2010 at 3:51 AM, Christopher Brewster
<cbre...@gmail.com> wrote:
> How do I test in Beautifulsoup if there is a string attribute, and if there is not do some further processing, and if there is get the string?

> I am always falling over this AuttributeError.
>
> Thanks,
> Christopher
>
> --
> You received this message because you are subscribed to the Google Groups "beautifulsoup" group.
> To post to this group, send email to beauti...@googlegroups.com.
> To unsubscribe from this group, send email to beautifulsou...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/beautifulsoup?hl=en.
>
>

Christopher Brewster

unread,
Aug 8, 2010, 6:12:59 PM8/8/10
to beauti...@googlegroups.com, Christopher Brewster
Thank you for your suggestion but that does not really solve the problem.
The scenario is as follows:
Parsing an XML document, some tags contain just text so one can call .string attribute and get the string inside the tag.
Only sometimes the same tag has got other tags within it, so a call to the .string attribute results in this error.

My current solution is a little unwieldy but works. I have a function 'extract_from_tag(tag)' which iterates over each item in tag.contents, performs various checks and tests if tag.contents > 0 in which cased recursively calls the function.
Messy but works. A hack in the old sense.

If there is a better way, I would appreciate suggestions.

Christopher

Aaron DeVore

unread,
Aug 8, 2010, 6:28:30 PM8/8/10
to beauti...@googlegroups.com
Christopher,
If you're getting an error for an operation on NoneType then you're
working with None when you expect a Tag. If you were working with a
Tag that doesn't fit the requirements for a .string then .string would
be set to None. Somewhere, one of your accesses to the tree is
returning None, which indicates a failed tree search.

Also, any attribute access on Tag will succeed because an access that
isn't in __dict__ automatically does a find(name) search for a tag
with that name.

-Aaron DeVore

Reply all
Reply to author
Forward
0 new messages