2
votes

I wish to write the beautifulsoup form with this code:

soup = BeautifulSoup(con.content)
f = open('/*/*/Desktop/littletext.rtf','w')
f.write(str(soup))
f.close()

I get this error:

Traceback (most recent call last): File "///Desktop/test123.py", line 10, in f.write(soup) TypeError: must be str, not BeautifulSoup

Any ideas how to fix this? I tried to convert 'soup' to string but didn't work -- f.write(str(soup))

1
The code at the top of your question doesn't match the traceback. What does "didn't work" mean, exactly; what was the error message you actually got with f.write(str(soup))? - jonrsharpe
@jonrsharpe Traceback (most recent call last): File "/*/*/Desktop/test123.py", line 10, in <module> f.write(str(soup)) UnicodeEncodeError: 'ascii' codec can't encode character '\u2026' in position 28521: ordinal not in range(128) - Bob Ebert
So you have read any of the numerous existing questions on dealing with UnicodeEncodeErrors specifically or Unicode in Python generally, or e.g. docs.python.org/3/howto/unicode.html? - jonrsharpe

1 Answers

14
votes

found out my problem is that I had to use 'wb' in

f = open('/*/*/Desktop/littletext.rtf','wb')

and

f.write(str(soup))

had to be

f.write(soup.encode('utf-8'))