[英]How do I call globals() from another imported function in Python?
[英]How do I call the functions of the imported library from my function?
在尝试使用 BeautifulSoup 从某些网页获取信息时,有很多重叠的代码,所以我想将其设为一个函数,但我想在bs
中调用一个函数,例如find_all
和select
。 我该怎么做?
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”未定义
如果您像这样调用函数(您可能必须提供其他参数)
test('www','find_all')
您可以在函数中调用方法“find_all”,例如:
result = getattr(soup, function)(*lst)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.