Note that there are some explanatory texts on larger screens.

plurals
  1. POPython ElementTree find() using a wildcard?
    text
    copied!<p>I am parsing an XML feed in python to extract certain tags. My XML contains namespaces and this results in each tag containing a namespace followed by tag name. </p> <p>Here is the xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:rte="http://www.rte.ie/schemas/vod"&gt; &lt;id&gt;10038711/&lt;/id&gt; &lt;updated&gt;2013-01-24T22:52:43+00:00&lt;/updated&gt; &lt;title type="text"&gt;Reeling in the Years&lt;/title&gt; &lt;logo&gt;http://www.rte.ie/iptv/images/logo.gif&lt;/logo&gt; &lt;link rel="self" type="application/atom+xml" href="http://feeds.rasset.ie/rteavgen/player/playlist?type=iptv&amp;amp;showId=10038711" /&gt; &lt;category term="feed"/&gt; &lt;author&gt; &lt;name&gt;RTE&lt;/name&gt; &lt;uri&gt;http://www.rte.ie&lt;/uri&gt; &lt;/author&gt; &lt;entry&gt; &lt;id&gt;10038711&lt;/id&gt; &lt;published&gt;2012-07-04T12:00:00+01:00&lt;/published&gt; &lt;updated&gt;2013-01-06T12:31:25+00:00&lt;/updated&gt; &lt;title type="text"&gt;Reeling in the Years&lt;/title&gt; &lt;content type="text"&gt;National and international events with popular music from the year 1989.First Broadcast: 08/11/1999&lt;/content&gt; &lt;category term="WEB Exclusive" rte:type="channel"/&gt; &lt;category term="Classics 1980" rte:type="genre"/&gt; &lt;category term="rte player" rte:type="source"/&gt; &lt;category term="" rte:type="transmision_details"/&gt; &lt;category term="False" rte:type="copyprotectionoptout"/&gt; &lt;category term="long" rte:type="form"/&gt; &lt;category term="3275" rte:type="progid"/&gt; &lt;link rel="site" type="text/html" href="http://www.rte.ie/tv50/"/&gt; &lt;link rel="self" type="application/atom+xml" href="http://feeds.rasset.ie/rteavgen/player/playlist/?itemId=10038711&amp;amp;type=iptv&amp;amp;format=xml" /&gt; &lt;link rel="alternate" type="text/html" href="http://www.rte.ie/player/#v=10038711"/&gt; &lt;rte:valid start="2012-07-23T15:56:04+01:00" end="2017-08-01T15:56:04+01:00"/&gt; &lt;rte:duration ms="842205" formatted="0:10"/&gt; &lt;rte:statistics views="19"/&gt; &lt;rte:bri id="na"/&gt; &lt;rte:channel id="13"/&gt; &lt;rte:item id="10038711"/&gt; &lt;media:title type="plain"&gt;Reeling in the Years&lt;/media:title&gt; &lt;media:description type="plain"&gt;National and international events with popular music from the year 1989. First Broadcast: 08/11/1999&lt;/media:description&gt; &lt;media:thumbnail url="http://img.rasset.ie/00062efc200.jpg" height="288" width="512" time="00:00:00+00:00"/&gt; &lt;media:teaserimgref1x1 url="" time="00:00:00+00:00"/&gt; &lt;media:rating scheme="http://www.rte.ie/schemes/vod"&gt;NA&lt;/media:rating&gt; &lt;media:copyright&gt;RTÉ&lt;/media:copyright&gt; &lt;media:group rte:format="single"&gt; &lt;media:content url="http://vod.hds.rasset.ie/manifest/2012/0728/20120728_reelingint_cl10038711_10039316_260_.f4m" type="video/mp4" medium="video" expression="full" duration="842205" rte:format="content"/&gt; &lt;/media:group&gt; &lt;rte:ads&gt; &lt;media:content url="http://pubads.g.doubleclick.net/gampad/ads?sz=512x288&amp;amp;iu=%2F3014%2FP_RTE_TV50_Pre&amp;amp;ciu_szs=300x250&amp;amp;impl=s&amp;amp;gdfp_req=1&amp;amp;env=vp&amp;amp;output=xml_vast2&amp;amp;unviewed_position_start=1&amp;amp;url=[referrer_url]&amp;amp;correlator=[timestamp]" type="text/xml" medium="video" expression="full" rte:format="advertising" rte:cue="0" /&gt; &lt;media:content url="http://pubads.g.doubleclick.net/gampad/ads?sz=512x288&amp;amp;iu=%2F3014%2FP_RTE_TV50_Pre2&amp;amp;ciu_szs=300x250&amp;amp;impl=s&amp;amp;gdfp_req=1&amp;amp;env=vp&amp;amp;output=xml_vast2&amp;amp;unviewed_position_start=1&amp;amp;url=[referrer_url]&amp;amp;correlator=[timestamp]" type="text/xml" medium="video" expression="full" rte:format="advertising" rte:cue="0" /&gt; &lt;media:content url="http://pubads.g.doubleclick.net/gampad/ads?sz=512x288&amp;amp;iu=%2F3014%2FP_RTE_TV50_Pre3&amp;amp;ciu_szs=300x250&amp;amp;impl=s&amp;amp;gdfp_req=1&amp;amp;env=vp&amp;amp;output=xml_vast2&amp;amp;unviewed_position_start=1&amp;amp;url=[referrer_url]&amp;amp;correlator=[timestamp]" type="text/xml" medium="video" expression="full" rte:format="advertising" rte:cue="0" /&gt; &lt;/rte:ads&gt; &lt;/entry&gt; &lt;!-- playlist.xml --&gt; &lt;/feed&gt; </code></pre> <p>When the XML is parsed each element is soming out as:</p> <pre><code>{http://www.w3.org/2005/Atom}id {http://www.w3.org/2005/Atom}published {http://www.w3.org/2005/Atom}updated ..... ..... {http://www.rte.ie/schemas/vod}valid {http://www.rte.ie/schemas/vod}duration .... .... {http://search.yahoo.com/mrss/}description {http://search.yahoo.com/mrss/}thumbnail .... </code></pre> <p>As I have 3 different namespaces and I cannot gurantee that they will be always the same then I woul prefer not to hard specify each tag like so:</p> <pre><code>for elem in tree.iter({http://www.w3.org/2005/Atom}entry'): stream = str(elem.find('{http://www.w3.org/2005/Atom}id').text) date_tmp = str(elem.find('{http://www.w3.org/2005/Atom}published').text) name_tmp = str(elem.find('{http://www.w3.org/2005/Atom}title').text) short_tmp = str(elem.find('{http://www.w3.org/2005/Atom}content').text) channel_tmp = elem.find('{http://www.w3.org/2005/Atom}category', "channel") channel = str(channel_tmp.get('term')) icon_tmp = elem.find('{http://search.yahoo.com/mrss/}thumbnail') icon_url = str(icon_tmp.get('url')) </code></pre> <p>Is there any way that I can put a wildcard or something similar into the find so it will simply ignore the namespace?</p> <pre><code>stream = str(elem.find('*id').text) </code></pre> <p>I can hardcode them as above but it would be my luck that down the line the namespace would change and my queries stop returning data..</p> <p>Thanks for the help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload