xml - lxml,Python. I can't get content of element: element.text is None -


I loaded an XML file from the Internet, and parsed it with LXML. But I could not find the content from the 'element' .text Both the results and the source are low, I can just write them down.

XML:

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Products & gt; & Lt; Products & gt; & Lt; Company & gt; & Lt; [CDATA [Google]] & gt; & Lt; / Company & gt; & Lt; Link & gt; & Lt;! [CDATA [http://www.google.com]] & gt; & Lt; / Link & gt; & Lt; Subject & gt; & Lt; [CDATA [Sushi]] & gt; & Lt; / Subject & gt; & Lt; / Products & gt; & Lt; / Products & gt;  

code:

  import from urllib2 to lxml import entry from StringIO import StringIO rss = urllib2.urlopen ("http://dizzy-v.co Kr / test / test.xml "). Read () tree = etree.parse (StringIO (rss), etree.HTMLParser ()) root = tree.getroot () root.iter ('product') for the product: Element.iter () element in: Print element.text  

Result:

  nobody at all  

Extract etree.HTMLParser gives you a texts:

  & gt; & Gt; & Gt; Import urllib2 & gt; & Gt; & Gt; From the Lxml Import Entry & gt; & Gt; & Gt; & Gt; & Gt; & Gt; RSS = urllib2.urlopen ("http://dizzy-v.co.kr/test/test.xml") .read () & gt; & Gt; & Gt; Root = etree.fromstring (rss) # & lt; ---- & gt; & Gt; & Gt; For product in root.iter ('product'): ... for element in product.iter (): ... print element.text ... google http://www.google.com sushi  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -