import urllib, urllib2
from bs4 import BeautifulSoup, Comment
strg=""
iter=1
url='http://www.amazon.in/product-reviews/B00EOPJEYK/ref=cm_cr_pr_top_link_1? ie=UTF8&pageNumber=1&showViewpoints=0&sortBy=bySubmissionDateDescending'
content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content, "html.parser")
rows =soup.find_all('div',attrs={"class" : "reviewText"})
for row in soup.find_all('div',attrs={"class" : "reviewText"}):
strg = strg +str(iter)+"." + row.text + "\n\n"
iter=iter+1
with open('outp.txt','w') as f:
f.write(strg)
f.close()
I require this code to write the contents of the variable,strg to the file,outp.txt.
Instead I get this error:
Traceback (most recent call last):
File "C:\Python27\demo_amazon.py", line 14, in <module>
f.write(strg)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2022' in position 226: ordinal not in range(128)
strg stores the required output.There is some problem in the writing statement I guess.How to solve this?
Kindly help.
Thank you.