我正在嘗試使用一個函式回傳的值作為另一個函式的引數值 - 在這種情況下,讓頁面在 selenium 中打開。但是,在函式之外,它無法識別回傳的值:
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)
user_profile_url 由 open_profile 函式回傳,非常適合通過 driver.get 函式傳遞。但是,這不起作用。
open_profile 函式
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
uj5u.com熱心網友回復:
您需要在使用它之前分配回傳值
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)
uj5u.com熱心網友回復:
您可以將 open_profile() 分配給某個變數,然后將其傳遞給 get() 或簡單地...
self.driver.get(self.open_profile(card))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/506711.html
上一篇:硒找不到元素
下一篇:selenium.common.exceptions.ElementClickInterceptedException:訊息:元素點擊被攔截:元素<arel="next"hr
