I wrote the following python script for retrieving the information from Yahoo Finance website and store it in a file. Following is the script:
import urllib.request
from bs4 import BeautifulSoup
in_data = open('list_of_companies.txt','r',encoding='utf-8', errors='ignore')
for line in in_data:
page = urllib.request.urlopen('http://finance.yahoo.com/rss/headline?s='+line.strip())
page = page.read()
page = page.decode('utf-8','ignore')
soup = BeautifulSoup(page)
ans = line[0:len(line.strip())]
ans += '.txt'
f1 = open(ans,'w')
f1.write(soup.prettify())
f1.close()
It will take input symbol from list_of_companies.txt
and store it in a file with that name. However, when i am running this script, this is giving me following error:
Traceback (most recent call last):
File "C:\Users\Darshil Babel\Desktop\NewsContent\s.py", line 12, in <module>
f1.write(soup.prettify())
File "C:\Python34\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u03cb' in position 1556: character maps to <undefined>
I am using urllib and BeautifulSoup module for this purpose. Can somebody help me with this?