Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6655021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:27:44+00:00 2026-05-26T01:27:44+00:00

I wrote a little wrapper using beautifulsoup great html parser recently I tried to

  • 0

I wrote a little wrapper using beautifulsoup great html parser

recently I tried to improve the code and make all beautifulsoup methods available directly in the wrapper class (instead of through a class property ) and I thought subclassing the beautifulsoup parser would be the best way to achieve this.

Here is the the class:

class ScrapeInputError(Exception):pass
from BeautifulSoup import BeautifulSoup

class Scrape(BeautifulSoup):
    """base class to be subclassed
    basically a subclassed BeautifulSoup wrapper that providers
    basic url fetching with urllib2
    and the basic html parsing with beautifulsoup
    and some basic cleaning of head,scripts etc'"""

    def __init__(self,file):
        self._file = file
        #very basic input validation
        import re
        if not re.search(r"^http://",self._file):
            raise ScrapeInputError,"please enter a url that starts with http://"

        import urllib2
        #from BeautifulSoup import BeautifulSoup
        self._page = urllib2.urlopen(self._file) #fetching the page
        BeautifulSoup.__init__(self,self._page)
        #self._soup = BeautifulSoup(self._page) #calling the html parser

this way I can just initiate the class with

x = Scrape("http://someurl.com")

and be able to traverse the tree with x.elem or x.find

this works wonderfull with some beautifulsoup methods (see above) but fails with others – those using iterator like “for e in x:”

the error message:

 Traceback (most recent call last):
  File "<pyshell#86>", line 2, in <module>
    print e
  File "C:\Python27\lib\idlelib\rpc.py", line 595, in __call__
    value = self.sockio.remotecall(self.oid, self.name, args, kwargs)
  File "C:\Python27\lib\idlelib\rpc.py", line 210, in remotecall
    seq = self.asynccall(oid, methodname, args, kwargs)
  File "C:\Python27\lib\idlelib\rpc.py", line 225, in asynccall
    self.putmessage((seq, request))
  File "C:\Python27\lib\idlelib\rpc.py", line 324, in putmessage
    s = pickle.dumps(message)
  File "C:\Python27\lib\copy_reg.py", line 77, in _reduce_ex
    raise TypeError("a class that defines __slots__ without "
TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled

I researched the error message but couldn’t find anything I could work with – becasue I don’t want to play with the inner implantation of BeautifulSoup (and honestly I don’t know or understand __slot__ or __getstate__..) I just want to use the functionality.

instead of subclassing I tried returning a beautifulsoup object from the __init__ of the class but __init__ method returns None

Be glad for any help here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:27:44+00:00Added an answer on May 26, 2026 at 1:27 am

    The error is not happening in BeautifulSoup code. Rather, your IDLE is not able to retreive and print the object. Try print str(e) instead.


    Anyway, subclassing BeautifulSoup in your situation may not be the best idea. Do you really want to inherit all of the parsing methods (like convert_charref, handle_pi or error)? Worse, if you override something that BeautifulSoup uses, it may break in a hard-to-find way.

    I don’t know your situation, but I suggest preferring composition over inheritance (i.e. having a BeautifulSoup object in an attribute). You can easily (if in a slightly hacky way) expose specific methods like this:

    class Scrape(object):
        def __init__(self, ...):
            self.soup = ...
            ...
            self.find = self.soup.find
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wrote a little addin, which does some formatting of my C# code. in
Some time ago I wrote a little piece of code to ask about on
I wrote this tiny little wrapper around order , but I fear my implementation
I wrote a little application using GAE and the playframework. I am trying to
I wrote a little code to get some tooltips in my multiple select box,
I wrote a little C++ wrapper around some parts of GSL and encounter the
I wrote a little program using fork to create new processes which use pipe
I wrote little WPF application with 2 threads - main thread is GUI thread
I wrote a little daemon in Perl that calls up FFMpeg to encode a
i wrote a little test programm which queries the status servlet of a tomcat

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.