Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 Write to file beautifulsoup

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))

like image 944
Bob Ebert Avatar asked Mar 15 '26 08:03

Bob Ebert


1 Answers

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'))
like image 94
Bob Ebert Avatar answered Mar 21 '26 16:03

Bob Ebert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!