title: Python requests: UnicodeEncodeError: 'charmap' codec can't encode character
body: <p>I scraped a webpage (name changed in code here) as follows:</p>
  
  <pre><code>import requests
  r = requests.get('https://www.samplewebpage.com')
  </code></pre>
  
  <p>Then I tried to write r.text to a file as follows:</p>
  
  <pre><code>f = open ('filename', 'w')
  f.write(r.text)
  f.close()
  </code></pre>
  
  <p>I get an error as:</p>
  
  <pre><code>UnicodeEncodeError: 'charmap' codec can't encode character '\u20b9' in position 158691: character maps to &lt;undefined&gt;
  </code></pre>
  
  <p>r.encoding shows UTF-8. How to resolve the above?</p>
  
  <p>Have also tried the following:
  - few other random webpages and am able to run the code without any error for most.
  - instead of r.text used r.content.decode('utf-8', 'ignore') but same error as above</p>
  
  <p>My environment/system specifications:</p>
  
  <ul>
  <li>Python 3.6.4</li>
  <li>Windows 8.1 Pro, 64 bit</li>
  <li>Default IDLE as installed from <a href="https://www.python.org" rel="nofollow noreferrer">https://www.python.org</a>.</li>
  <li>Tried with a script in Atom as well, but same error.</li>
  </ul>
  
  <p>Suspecting console encoding mismatch as I read in another similar problem on this forum, I reconfirmed from that the Atom console is set to UTF-8, though I believe console encoding is not the problem here, as I want to write to a file.</p>
  
  <p>Thanks</p>