title: Writing BeautifulSoup contents to a file
body: <p>I recently asked <a href="https://stackoverflow.com/questions/14412907/handling-indian-languages-in-beautifulsoup/14412961#14412961">this</a> question about encoding Hindi characters in BeautifulSoup.
  The answers to that question did solve that issue, however I have another problem.</p>
  
  <p>My code is :</p>
  
  <pre><code>import urllib2
  from bs4 import BeautifulSoup
  
  htmlUrl = "http://archives.ndtv.com/articles/2012-01.html"
  FileName = "NDTV_2012_01.txt"
  
  fptr = open(FileName, "w")
  fptr.seek(0)
  
  page = urllib2.urlopen(htmlUrl)
  soup = BeautifulSoup(page, from_encoding="UTF-8")
  
  li = soup.findAll( 'li')
  for link_tag in li:
    hypref = link_tag.find('a').contents[0]
    strhyp = hypref.encode('utf-8')
    fptr.write(strhyp)
    fptr.write("\n")
  </code></pre>
  
  <p>And I get an error </p>
  
  <pre><code>Traceback (most recent call last):
  File "./ScrapeTemplate.py", line 29, in &lt;module&gt;
  hypref = link_tag.find('a').contents[0]
  IndexError: list index out of range
  </code></pre>
  
  <p>It seems to work when I substitute <code>print strhyp</code> instead of <code>fptr.write()</code>. How do I fix this?</p>
  
  <p><strong>EDIT:</strong> There was a mistake in the code which I didn't spot. Fixed it, but I'm still getting the same error.</p>