I have made this code:
from newspaper import Article
url = 'https://en.wikipedia.org/wiki/United_States'
article = Article(url)
article.download()
article.parse()
print(article.text)
It correctly prints the wikipedia article to the screen when run, but I want it to save the contents (essentially the screen output) in a .txt file on my computer.
I thought this would be possible by adding:
import sys
with open('output.txt', 'w') as f:
sys.stdout = f
print(article.text)
sys.stdout = sys.__stdout_
However, when I run this, I get an error:
UnicodeEncodeError: 'charmap' codec can't encode character '\u2032' in position 16074: character maps to <undefined>
Why is this error occuring when purely the text data from webpage has been extracted? What can I do to resolve the error?
Thank you