我想将函数def elliptic()的第一个结果插入到第二个函数entity_noun()中。在第二个函数中,查找具有特定值的属性的节点。我希望从第一个函数的返回值中检索这个值(在引号"??????"中是一个字符串)。
from bs4 import BeautifulSoup
def elliptic():
last_a_tag = soup.find_all("sn", elliptic="yes")
for item in last_a_tag:
entity = item.get('entity')
return(entity)
def entity_noun():
ent = soup.find(entity="??????")
noun = ent.find('n')
return(noun)你有什么建议怎么做吗?
发布于 2018-04-17 09:37:10
您可以在参数中传递调用函数的结果。
所以在这种情况下,你可以做:
ent = soup.find(entity=elliptic())发布于 2018-04-17 09:36:00
你这里有两个功能。函数来返回结果。如果你这样做:
from bs4 import BeautifulSoup
def elliptic():
last_a_tag = soup.find_all("sn", elliptic="yes")
for item in last_a_tag:
entity = item.get('entity')
return(entity)
def entity_noun():
ent = soup.find(entity=elliptic())
noun = ent.find('n')
return(noun)
entity_noun()您将调用entity_noun(),后者将调用elliptic()
另一种选择是使用参数:
from bs4 import BeautifulSoup
def elliptic():
last_a_tag = soup.find_all("sn", elliptic="yes")
for item in last_a_tag:
entity = item.get('entity')
return(entity)
def entity_noun(X):
ent = soup.find(entity=X)
noun = ent.find('n')
return(noun)
A=elliptic()
entity_noun(A)在本例中,您将调用第一个函数elliptic(),将结果保存在A中,然后将A传递给entity_noun()。使用第二种方法,每个函数将保持独立于另一个函数,因此在不同的上下文中独立使用。
https://stackoverflow.com/questions/49874447
复制相似问题