How to parse a tag named 'name'?

83 views
Skip to first unread message

Giorgos Tsiapaliwkas

unread,
Jul 23, 2012, 9:37:04 AM7/23/12
to beauti...@googlegroups.com
Hello,

I don't know if this is the right place to ask for help. If it isn't I apologize.

I have this xml,

<project identifier="foo">
<name>a simple text</name>
<path>some/path</path>
   ......

and this is my code

 for project in self.soup.find_all("project"):
                projectPath = project.path.string.split('/')
                print projectPath[0] #is ok
                print project.name.string# error AttributeError: 'str' object has no attribute 'string'

I can't parse the tag named 'name'. I have tried the rest of them and they work.
Is this relevant with the fact that bs provided already the name attribute?

Regards,
Giorgos

--
Giorgos Tsiapaliwkas (terietor)
KDE Developer

terietor.gr

Leonard Richardson

unread,
Jul 23, 2012, 10:01:47 AM7/23/12
to beauti...@googlegroups.com
> I don't know if this is the right place to ask for help. If it isn't I
> apologize.
>
> I have this xml,
>
> <project identifier="foo">
> <name>a simple text</name>
> <path>some/path</path>
> ......
>
> and this is my code
>
> for project in self.soup.find_all("project"):
> projectPath = project.path.string.split('/')
> print projectPath[0] #is ok
> print project.name.string# error AttributeError: 'str'
> object has no attribute 'string'
>
> I can't parse the tag named 'name'. I have tried the rest of them and they
> work.
> Is this relevant with the fact that bs provided already the name attribute?

Yes. In your example, `project.name` is the string "project". To find
a tag called <name>, use the find() method:

project.find("name")
http://www.crummy.com/software/BeautifulSoup/bs4/doc/#find

Leonard

Jorge Cribb

unread,
Jul 23, 2012, 10:15:34 AM7/23/12
to beauti...@googlegroups.com
May be this help you

for project in soup.find_all("project"):
    projectPath = project.path.string.split('/')
    print projectPath[0]
    for nName in project.find_all("name"):
        print nName.string

Giorgos Tsiapaliwkas

unread,
Jul 24, 2012, 9:12:19 AM7/24/12
to beauti...@googlegroups.com


On Monday, July 23, 2012 5:15:34 PM UTC+3, Jorge Cribb wrote:
May be this help you

for project in soup.find_all("project"):
    projectPath = project.path.string.split('/')
    print projectPath[0]
    for nName in project.find_all("name"):
        print nName.string

thanks it worked :) 
Reply all
Reply to author
Forward
0 new messages