0

I want to write the HTML of a website to the file I created, tough I decode to utf-8 but still it puts up a error like this, I use print(data1) and the html is printed properlyand I am using python 3.5.0

import re
import urllib.request

city = input("city name")   
url = "http://www.weather-forecast.com/locations/"+city+"/forecasts/latest"
data  = urllib.request.urlopen(url).read()
data1 = data.decode("utf-8")
f = open("C:\\Users\\Gopal\\Desktop\\test\\scrape.txt","w")
f.write(data1)
am guru prasath
  • 345
  • 1
  • 7
  • 28
  • unrelated: (1) use `urllib.parse.quote(city, safe='')` (2) in general, the server may use a different character encoding, see [A good way to get the charset/encoding of an HTTP response in Python](http://stackoverflow.com/q/14592762/4279) (3) use `with`-statement to close the file, otherwise nothing might be written to disk due to buffering. – jfs Nov 03 '15 at 02:40

2 Answers2

3

You've opened a file with the default system encoding:

f = open("C:\\Users\\Gopal\\Desktop\\test\\scrape.txt", "w")

You need to specify your encoding explicitly:

f = open("C:\\Users\\Gopal\\Desktop\\test\\scrape.txt", "w", encoding='utf8')

See the open() function documentation:

In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding.

On your system, the default is a codec that cannot handle your data.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0
f = open("C:\\Users\\Gopal\\Desktop\\test\\scrape.txt","w",encoding='utf8')


f.write(data1)

This should work, it did for me

Artemis
  • 2,553
  • 7
  • 21
  • 36
vikas
  • 1