title: How to save the BeautifulSoup object to a file and then read from it as BeautifulSoup?
body: <p>I want to save the BeautifulSoup object to a file. So, I change it into a string, then write it to a file. Then after reading it as a string, I convert the string into a BeautifulSoup object. This would help during my testing as the data I am scraping is dynamic.</p>
  
  <pre class="lang-py prettyprint-override"><code>url = "https://coinmarketcap.com/all/views/all/"
  html = urlopen(url)
  soup = BeautifulSoup(html,"lxml")
  </code></pre>
  
  <p>Writing the soup object like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>  new_soup = str(soup)
    with open("coin.txt", "w+") as f:
        f.write(new_soup)
  </code></pre>
  
  <p>produces this error:</p>
  
  <pre><code>UnicodeEncodeError: 'charmap' codec can't encode 
  characters in position 28127-28132: character maps to &lt;undefined&gt;
  </code></pre>
  
  <p>Also, if I am able to save it to a file, how would I read the string returned as a BeautifulSoup object?</p>