【问题标题】:How do I call the functions of the imported library from my function?如何从我的函数中调用导入库的函数?
【发布时间】:2022-07-01 21:17:40
【问题描述】:

在尝试使用BeautifulSoup从一些网页获取信息时,有很多重叠的代码,所以我想把它变成一个函数,但我想在bs中调用一个函数,例如find_allselect .我该怎么做?

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:名称“find_all”未定义

【问题讨论】:

标签: python


【解决方案1】:

如果你像这样调用函数

test('www','find_all')

您可以在函数中调用“find_all”方法,如:

result = getattr(soup, function)(*lst)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-21
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多