title: Save Contents of URL to Text File
body: <p>I'm trying to save the contents of a URL to a text file.  I found several some sample scripts online to do this, and the two below seem like good candidates to help me do what I want to do, but both return this error:</p>
  <blockquote>
  <p>TypeError: a bytes-like object is required, not 'str'</p>
  </blockquote>
  <pre><code>import html2text
  import urllib.request
  
  with urllib.request.urlopen(&quot;http://www.msnbc.com&quot;) as r:
      html_content = r.read()
  rendered_content = html2text.html2text(html_content)
  file = open('C:\\Users\\Excel\\Desktop\\URL.txt', 'w')
  file.write(rendered_content)
  file.close()
  
  
  
  import sys
  if sys.version_info[0] == 3:
      from urllib.request import urlopen
  else:
      # Not Python 3 - today, it is most likely to be Python 2
      # But note that this might need an update when Python 4
      # might be around one day
      from urllib import urlopen
  # Your code where you can use urlopen
  with urlopen(&quot;http://www.msnbc.com&quot;) as r:
      s = r.read()
  rendered_content = html2text.html2text(html_content)
  file = open('C:\\Users\\Excel\\Desktop\\URL.txt', 'w')
  file.write(rendered_content)
  file.close()
  </code></pre>
  <p>I'm probably missing something simple here, but I can't tell what it is.</p>
  <p>I am using Python 3.6.</p>