Findall equivalent for xpath , Lxml

I am extracting text with respect to tags and I need to get them in a list form w.r.t p tags. I have this xpath expression as:

 find =  etree.XPath("//w:p//.//*[local-name() = 'ins']//text()" ,namespaces={'w':""}) 

And i want to use it in a findall expression. I tried:

for p in lxml_tree.findall('.//{' + w + '}p'):
    inserted_list_1.append([t.text for t in p.findall('.//{' + w + '}ins')])

but all this returns is a list full of None values whilst the former xpath works perfectly. I think there's some intermediate path missing.


You cannot use that expression with findall(); the findall() method deliberately keeps compatibility with the limited ElementTree API XPath support.

Use the xpath() method instead:

for p in lxml_tree.xpath('.//w:p', namespaces={'w': w}):

and just use namespace prefixes for much more readable queries.

If you just wanted to extract all contained text, you can use:

[t for t in p.xpath('../w:p//w:ins//text()',namespaces={'w': w})]

Need Your Help

Using forms to reload and update a PHP page with jQUERY/AJAX

php jquery html ajax

I want to post info to a PHP page from html inputs and reload it dynamically using jQUERY and AJAX. The function should take all the values from the page inputs and post them to the PHP page, then ...

Memory issue when storing images in byteArray

java android performance

I have an app that needs to access a large number of images very quickly, so I need to load those images into memory in some way. Doing so as bitmaps used over 100MB of RAM, which was completely o...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.