Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I call the functions of the imported library from my function?

While trying to get information from some web pages using BeautifulSoup, there are many overlapping codes, so I want to make it a function, but I want to call a function within bs such as find_all and select. How can I do it?

import requests
from bs4 import BeautifulSoup

def test(url, function, *lst):
    
    result = requests.get(url)
    soup = BeautifulSoup(result.text, "lxml")
    result = soup.function(*lst)
    return

test('www', find_all)
test('www', select_one)

NameError: name ‘find_all’ is not defined

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you call the function like (you may have to provide additional arguments)

test('www','find_all')

You can call the method "find_all" in the function like:

result = getattr(soup, function)(*lst)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading