python - 使用带有 webdriver.find 函数的 python 过滤与 selenium 进行网络抓取

标签 python html selenium web-scraping

我正在过滤出现在这个网站上的固定 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 找到它。查找函数。

这是我正在寻找的提示:

https://imgur.com/a/8B5TrMe

想法是获取这个数量的卡片并在循环中使用它来获取聚合在向量中的卡片信息。

    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/

相关文章:

python - 在python中将包装为字符串的元组转换为元组的简单方法

php - 将图像 PHP 上传到 MySQL 并检索正确的图像到每个表单

python - 获取 html 属性值,属性名称包含冒号和 xpath 表达式

python - 使用没有 ID 的 Selenium 查找并单击按钮

java - 尝试通过 Selenium 和 Java 使用 sendKeys 时获取验证消息

python错误类型错误: not all arguments converted during string formatting

python - 从 Python 中的展平数据生成嵌套列表

html - 如何在 Bootstrap 中的卡之间添加填充

python - PySpark - Hive 上下文不返回结果但 SQL 上下文返回类似查询

html - 为什么在空 html 文件中应用边框时通用选择器显示两个元素? (CSS)