Esempio n. 1
0
def load_ipython_extension(ipython):
    print "Monkey patch BeautifulSoup with custom rendering"
    BeautifulSoup._repr_html_ = render
    Tag._repr_html_ = render

    if bs4:
        BeautifulSoup.find_all = wrap_findall(BeautifulSoup.find_all)
        Tag.find_all = wrap_findall(Tag.find_all)
    else:
        BeautifulSoup.findAll = wrap_findall(BeautifulSoup.findAll)
        Tag.findAll = wrap_findall(Tag.findAll)

    to_push = ["BeautifulSoup", "urlopen", "p"]
    print "Push 'BeautifulSoup' of '%s' into current context" % \
        ("bs4" if bs4 else "BeautifulSoup")
    print "Push 'urlopen' of 'urllib2' into current context"
    print "Push 'p' shortcut into current context"
    try:
        import requests
        print "Push 'requests' into current context"
        to_push.append('requests')
    except ImportError:
        pass
    ipython.push(to_push)


if __name__ == '__main__':
    BeautifulSoup._repr_html_ = render
    soup = BeautifulSoup(open("test.html").read())
    soup._repr_html_()