View Single Post
Old 06-12-2019, 07:44 PM   #8
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,384
Karma: 27756918
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
For new recipes it does not matter, since new recipes are only available once a new release of calibre is made, just use bs4.

For updating old recipes, one just has to be a bit careful to write code that works for both. The main incompatibilities are creating Tag objects and directly check class values

For creating Tag objects, use this wrapper function, which works with both bs3 and bs4.

Code:
def new_tag(soup, name, attrs=()):
    impl = getattr(soup, 'new_tag', None)
    if impl is not None:
        return impl(name, attrs=dict(attrs))
    return Tag(soup, name, attrs=attrs or None)
If you wish to directly check if the clas attribute of a tag has a value, use somethinglike:

Code:
def check_in(tag, attr, val):
   q = tag[attr]
   if not isinstance(q, list):
       q = q.split()
   return attr in q
kovidgoyal is online now   Reply With Quote