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

Andreas Perstinger andreas.perstinger at gmx.net
Mon Nov 14 07:06:00 EST 2011


On 2011-11-13 23:37, goldtech wrote:
> 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.
 >>> from BeautifulSoup import BeautifulSoup
 >>> html = "<html><body>Demo"
 >>> soup = BeautifulSoup(html)
 >>> type(soup)
<class 'BeautifulSoup.BeautifulSoup'>

To write the modified document into a file, you have to convert this 
structur back into a string:

 >>> new_html = str(soup)
 >>> type(new_html)
<type 'str'>
 >>> new_html
'<html>\n <body>\n  Demo\n </body>\n</html>'

HTH, Andreas



More information about the Python-list mailing list