简体   繁体   English

使用返回值作为 function 参数

[英]Using a returned value as a function argument

I'm trying to use a value returned by one function as the argument value for another function - in this case, getting the page to open in selenium.我正在尝试使用一个 function 返回的值作为另一个 function 的参数值 - 在这种情况下,让页面在 selenium 中打开。 However, outside of the function it does not recognise the value returned:但是,在 function 之外,它无法识别返回的值:

def discover(self, terms):
    self.open_browser()
    for term in terms:
        self.search(term)
        time.sleep(2)
        html = BeautifulSoup(self.driver.page_source, 'lxml')
        time.sleep(0.5)
        #self.scroll(html)
        cards = html.find_all('div', class_='styles__UserCardInformation-sc-f909fw-5 jEfkYy')
        #print(cards)
        time.sleep(0.5)
        for card in cards:
            self.open_profile(card)
            self.driver.get(user_profile_url)

The user_profile_url is returned by the open_profile function, ideal to be passed through the driver.get function. user_profile_url 由 open_profile function 返回,非常适合通过 driver.get function 传递。 However, this doesn't work.但是,这不起作用。

open_profile function open_profile function

def open_profile(self, card):
    user = card.div.span.a.p.text
    user_link_suffix = card.div.span.a['href']
    user_profile_url = f'https://www.mixcloud.com{user_link_suffix}'
    print(user)
    return user_profile_url

you need to assign the return value before using it您需要在使用它之前分配返回值

 def discover(self, terms):
self.open_browser()
for term in terms:
    self.search(term)
    time.sleep(2)
    html = BeautifulSoup(self.driver.page_source, 'lxml')
    time.sleep(0.5)
    #self.scroll(html)
    cards = html.find_all('div', class_='styles__UserCardInformation-sc-f909fw-5 jEfkYy')
    #print(cards)
    time.sleep(0.5)
    for card in cards:
        user_profile_url = self.open_profile(card)
        self.driver.get(user_profile_url)

You can either assign the open_profile() to some variable and then pass that to get() or simply...您可以将 open_profile() 分配给某个变量,然后将其传递给 get() 或简单地...

self.driver.get(self.open_profile(card))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM