How to loop through Beautiful Soup elements to get attribute values

saad1991

New Member
I need to iterate over Beautiful Soup elements and get the attribute values:For a XML doc: \[code\]<?xml version="1.0" encoding="UTF-8"?><Document> <Page x1="71" y1="120" x2="527" y2="765" type="page" chunkCount="25" pageNumber="1" wordCount="172"> <Chunk x1="206" y1="120" x2="388" y2="144" type="unclassified"> <Word x1="206" y1="120" x2="214" y2="144" font="Times-Roman" style="font-size:22pt">K</Word> <Word x1="226" y1="120" x2="234" y2="144" font="Times-Roman" style="font-size:22pt">O</Word> </Chunk> </Page></Document>\[/code\]I would like to get the x1 values of the "Word" elements (206,226).Help much appriciated!EDIT:I have tried:\[code\]for i in soup.page.chunk: i.word['x1']\[/code\]that returns an error:\[code\]File "C:\Python26\lib\site-packages\BeautifulSoup.py", line 473, in __getattr__ raise AttributeError, "'%s' object has no attribute '%s'" % (self.__class__.__name__, attr)AttributeError: 'NavigableString' object has no attribute 'word'\[/code\]while:\[code\]soup.page.chunk.word['x1']\[/code\]works correctly...and:\[code\]for i in soup.page.chunk: i.findNext(text=True)\[/code\]gets the text form the element.
 
Back
Top