Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Trying to write beautifulsoup result to a file and get error message

Reply
Thread Tools

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

 
 
goldtech
Guest
Posts: n/a
 
      11-13-2011
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
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      11-13-2011
On 13/11/2011 22: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.
>

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?
 
Reply With Quote
 
 
 
 
Andreas Perstinger
Guest
Posts: n/a
 
      11-14-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ofstream Error: Failed to write file: Result is too large. sharat C++ 1 04-16-2012 09:57 PM
py2exe with BeautifulSoup and simplejson error Gabriel Rossetti Python 0 04-22-2009 11:02 AM
lookup message trying to write to a file juliatiupsam@gmail.com C Programming 3 04-09-2007 08:58 PM
BeautifulSoup to get string inner 'p' and 'a' tags GinTon Python 3 07-24-2006 10:41 AM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM



Advertisments