title: XML Parsing with ElementTree and Requests
body: <p>I am trying to work with the Yahoo Weather API, but I am having a few issues parsing the <code>XML</code> that the API responds with. I am using <code>Python 3.4</code>. Here's the code I am working with:</p>
  
  <pre><code>weather_url = 'http://weather.yahooapis.com/forecastrss?w=%s&amp;u=%s'
  url = weather_url % (zip_code, units)
  
  try:
      rss = parse(requests.get(url, stream=True).raw).getroot()
  
      conditions = rss.find('channel/item/{%s}condition' % weather_ns)
  
      return {
          'current_condition': conditions.get('text'),
          'current_temp': conditions.get('temp'),
          'title': rss.findtext('channel/title')
      }
  except:
      raise
  </code></pre>
  
  <hr>
  
  <p>Here's the stack trace that I am getting:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;input&gt;", line 1, in &lt;module&gt;
    File "/home/jonathan/PycharmProjects/pyweather/pyweather/pyweather.py", line 42, in yahoo_conditions
      rss = parse(requests.get(url, stream=True).raw).getroot()
    File "/usr/lib/python3.4/xml/etree/ElementTree.py", line 1187, in parse
      tree.parse(source, parser)
    File "/usr/lib/python3.4/xml/etree/ElementTree.py", line 598, in parse
      self._root = parser._parse_whole(source)
    File "&lt;string&gt;", line None
  xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0
  </code></pre>
  
  <hr>
  
  <p>The xml.etree.ElementTree parse function doesn't like the raw object returned by the requests library. Looking into it a little bit deeper, the raw object resolves to</p>
  
  <pre><code>&gt;&gt;&gt; r = requests.get('http://weather.yahooapis.com/forecastrss?w=2502265', stream=True)
  &gt;&gt;&gt; r.raw
  &lt;requests.packages.urllib3.response.HTTPResponse object at 0x7f32c24f9e48&gt;
  </code></pre>
  
  <p>I referenced <a href="https://stackoverflow.com/a/16933729">this solution</a>, but it's still leading to the same issue. Why doesn't the approach above work? Is the urllib3 response object not supported with the ElementTree.parse function? I have read all of the docs, but they haven't enlightened me at all.</p>
  
  <p>The doc list is here:</p>
  
  <ul>
  <li><a href="http://docs.python.org/3.4/library/xml.etree.elementtree.html#xml.etree.ElementTree.parse" rel="nofollow noreferrer">xml.etree.ElementTree.parse doc</a></li>
  <li><a href="http://docs.python-requests.org/en/latest/api/#requests.Request" rel="nofollow noreferrer">requests.Request doc</a></li>
  <li><a href="http://urllib3.readthedocs.org/en/latest/helpers.html#urllib3.response.HTTPResponse" rel="nofollow noreferrer">urllib3.response.HTTPResponse doc</a></li>
  </ul>
  
  <p><strong>Edit:</strong>
  After more experimentation, I still haven't found a solution to the problem outlined above. However, I have found a workaround. If you use the ElementTree's <em>fromstring</em> method on the XML content, everything works fine.</p>
  
  <pre><code>def fetch_xml(url):
      """
      Fetch a url and parse the document's XML.
  
      :param url: the URL that the XML is located at.
      :return: the root element of the XML.
      :raises:
          :requests.exceptions.RequestException: Requests could not open the URL.
          :xml.etree.ElementTree.ParseError: xml.etree.ElementTree failed to parse the XML document.
      """
  
      return ET.fromstring(requests.get(url).content)
  </code></pre>
  
  <p>I guess the downside to this approach is that it uses more memory. What do you think? I'd like to get the communities opinion.</p>