I'm on macOS and i'm using Python 3.6 with Sublime Text 3. When i run my script i get the error in the title. I already tried everything i could find on the net to resolve this but i still have the same issue and no idea to to solve it. Here is my script:
import requests
import subprocess
import time
from bs4 import BeautifulSoup
response = requests.get("https://news.ycombinator.com")
soup = BeautifulSoup(response.content, "html.parser")
for story in soup.find_all(class_="storylink"):
title = story.get_text()
print(title + "\n")
Full stacktrace is:
The Land of Lisp
Traceback (most recent call last):
File "/Users/dave/Programming/Python/ReadHackerNews/read_hackernews.py", line 12, in <module>
Lost Laughs in Leisure Suit Larry
print(title + "\n")
UnicodeEncodeError: 'ascii' codec can't encode character '\u2013' in position 23: ordinal not in range(128)
[Finished in 1.2s]
the issue is with the title
variable and yes, i understand that it contains some unicode characters that python doesn't know how to print (because it uses ASCII???).
What i got working was, to print the unicode character in the form of 13\xc2\xa0comments
for example. But i want to print it as the unicode character...
If you run the script you have to have some "luck" to run into that issue since not every title on hackernews contains a unicode character. Also, the say
command is only present on macOS - remove it if you test on another OS.
EDIT: For fun i tried to execute the script in the terminal and there i don't get the error! So this has something to do with sublime text 3...
EDIT2: It works if i add sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())