我正在过滤出现在这个网站上的固定 yield 产品:https://yubb.com.br/investimentos/renda-fixa?investment_type=cdb&months=3&principal=10000000.0&sort_by=minimum_investment
基本上,该页面有一些卡片,我想知道每个页面出现了多少张卡片。例如选择cdb type和3 months,显示16张卡片,但再输入months或type of product,可能出现的卡片就少了。
到现在为止,我知道在查看“investmentCardContainer__footer”时它会出现多少页,这是一个类,但卡片的数量看起来像显示为样式,我不知道如何使用 selenium webdriver 找到它。查找函数。
这是我正在寻找的提示:
想法是获取这个数量的卡片并在循环中使用它来获取聚合在向量中的卡片信息。
vetor = ["cdb","lca","lci"]
dataset_boxes =[]
now = time.time()
for i in vetor:
options = Options()
options.add_argument('--headless')
url = 'https://yubb.com.br/investimentos/renda-fixa?investment_type={}&months=12\
&principal=1000000.0&sort_by=net_return'.format(i)
driver = webdriver.Chrome("C:\\Users\\yourpath\\Desktop\\PYTHON\\chromedriver.exe",options=options)
driver.get(url)
time.sleep(1)
num_pages = driver.find_element_by_class_name("investmentCardContainer__footer").text
list_pages = Convert(num_pages)
last_page = int(list_pages[len(list_pages)-3])
driver.quit()
for j in range(1,last_page+1):
url2 = 'https://yubb.com.br/investimentos/renda-fixa?collection_page={}&investment_type={}&months=12\
&principal=1000000.0&sort_by=net_return'.format(j,i)
driver = webdriver.Chrome("C:\\Users\\yourpath\\Desktop\\PYTHON\\chromedriver.exe",options=options)
driver.get(url2)
num_boxes = driver.find_element_by_class_name("investmentCardContainer__body").text
list_boxes = Convert(num_boxes)
dataset_boxes.append(list_boxes)
driver.quit()
print('idk')
later = time.time()
difference = int(later - now)
print('Processo finalizado em {} segundos.'.format(difference))
最佳答案
使用 WebDriverWait
并按照 xpath
获取 no of pages
计数。
print(WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'(//span[@class="page"]//a)[last()]'))).text)
你需要有以下导入来执行上面的代码。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
对于此链接:https://yubb.com.br/investimentos/renda-fixa?investment_type=cdb&months=3&principal=10000000.0&sort_by=minimum_investment
它应该返回:8
关于python - 使用带有 webdriver.find 函数的 python 过滤与 selenium 进行网络抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57119466/