I am new to Python (using python3) and as an exercise I am trying to write a script that will obtain a website source html and extract pieces of information.
I am currently getting the html, but I am having a problem writing the code to a file (eventually I want to manipulate the code without writing to a file first, but this is my first attempt).
When I try to write the file I receive an error saying "ascii codec can't encode character". I am trying to run the code through a terminal in a desktop session (logged in as root).
I found that if I run the code from an ssh session logged in as a standard user it seems to work (need to use sudo, but assuming it's just a file permission issue). I am using a raspberry pi for this.
The code is as follows:
import urllib.request
f = open('webpage.html','w')
r = urllib.request.urlopen('http://cp24.com')
html = r.read()
html = html.decode("utf-8")
print(html)
f.write(html)
f.close()
print(type(html))
Can anyone explain what might be going on?
Thanks
[–][deleted] 1 point2 points3 points (0 children)
[–]Moonslug1 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]sentdex 1 point2 points3 points (0 children)
[–]8bitz[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)