Trying to write beautifulsoup result to a file and get error message

G

goldtech

If I try:
....
soup = BeautifulSoup(ft3)
f = open(r'c:\NewFolder\clean4.html', "w")
f.write(soup)
f.close()

I get error message:

Traceback (most recent call last):
File "C:\Documents and Settings\user01\Desktop\py\tb1a.py", line
203, in <module>
f.write(soup)
TypeError: expected a character buffer object

I want to write beautiful soup's result to a file, I am doing
something wrong. Help appreciated.

Thanks
 
M

MRAB

If I try:
...
soup = BeautifulSoup(ft3)
f = open(r'c:\NewFolder\clean4.html', "w")
f.write(soup)
f.close()

I get error message:

Traceback (most recent call last):
File "C:\Documents and Settings\user01\Desktop\py\tb1a.py", line
203, in<module>
f.write(soup)
TypeError: expected a character buffer object

I want to write beautiful soup's result to a file, I am doing
something wrong. Help appreciated.
What do you mean by "beautiful soup's result"?

The original HTML is text, and you want it to write some text to the
file, but what exactly are you expecting it to write?
 
A

Andreas Perstinger

If I try:
...
soup = BeautifulSoup(ft3)
f = open(r'c:\NewFolder\clean4.html', "w")
f.write(soup)
f.close()

I get error message:

Traceback (most recent call last):
File "C:\Documents and Settings\user01\Desktop\py\tb1a.py", line
203, in<module>
f.write(soup)
TypeError: expected a character buffer object

I want to write beautiful soup's result to a file, I am doing
something wrong. Help appreciated.

BeautifulSoup takes a html document in the form of a string or file-like
oject and creates an internal data-structure after parsing it:

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.<class 'BeautifulSoup.BeautifulSoup'>

To write the modified document into a file, you have to convert this
structur back into a string:
'<html>\n <body>\n Demo\n </body>\n</html>'

HTH, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top