Selenium Web抓取没有ID或类名的嵌套div

如何解决Selenium Web抓取没有ID或类名的嵌套div

我正在尝试使用硒从嵌套的HTML表中获取产品名称和数量。我的问题是某些div没有任何ID或类名。我尝试访问的表是重要产品列表。这是我所做的,但是我似乎对如何获取嵌套的div感到迷茫。 该网站位于代码中。

options = Options()
options.add_argument('start-maximized')

driver = webdriver.Chrome(chrome_options=options,executable_path=r'/usr/local/bin/chromedriver/')
url = 'https://www.rrpcanada.org/#/' # site I'm scraping
driver.get(url)
time.sleep(150)
page = driver.page_source
driver.quit()


html_soup = BeautifulSoup(page,'html.parser')
item_containers = html_soup.find_all('div',class_='critical-products-title hide-mobile')

if item_containers:
    for item in item_containers:
       for link in item.findAll('a',) # need to loop the inner divs to reach the href and then get to the left and right classes to get title and quantity
        print(item)

这是检查的图像。我希望能够遍历所有div并获得标题和数量。

enter image description here

解决方法

您不需要漂亮的汤,也不需要保存page_source。 我使用CSS选择器选择表中的所有目标行,然后应用列表推导选择每行的左侧和右侧。我将结果输出到元组列表。

options = Options()
options.add_argument('start-maximized')

driver = webdriver.Chrome(chrome_options=options,executable_path=r'/usr/local/bin/chromedriver/')
url = 'https://www.rrpcanada.org/#/' # site I'm scraping
driver.get(url)
time.sleep(150)

elements = driver.find_elements_by_css_selector('#app > div:nth-child(1) > div.header-wrapper > div.header-right > div.critical-product-table-container > div.table.shorten.hide-mobile > div > div > div > a > div')

targetted_values = [(element.find_element_by_css_selector('.line-item-left').text,element.find_element_by_css_selector('.line-item-right').text) for element in elements]

driver.quit()

目标值的输出示例:

[('Surgical & Reusable Masks','376,713,363 available'),('Disposable Gloves','66,962,093 available'),('Gowns and Coveralls','40,502,145 available'),('Respirators','22,189,273 available'),('Surface Wipes','20,650,831 available'),('Face Shields','16,535,686 available'),('Hand Sanitizer','11,152,890 L available'),('Thermometers','8,457,993 available'),('Testing Kits','2,110,815 available'),('Surface Solutions','107,452 L available'),('Protective Barriers','10,833 available'),('Ventilators','410 available')]
,

要打印visibility_of_all_elements_located()所需的WebDriverWait的产品名称和数量,可以使用以下任一Locator Strategies

  • 使用CSS_SELECTOR text 属性:

    driver.get('https://www.rrpcanada.org/#/')
    items =  [my_elem.text for my_elem in WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"div.table.shorten.hide-mobile > div div.line-item-title")))]
    quantities =  [my_elem.text for my_elem in WebDriverWait(driver,"div.table.shorten.hide-mobile > div div.line-item-bold.available")))]
    for i,j in zip(items,quantities):
      print(i,j)
    
  • 使用XPATHget_attribute("innerHTML")

    driver.get('https://www.rrpcanada.org/#/')
    items =  [my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[@class='table shorten hide-mobile']/div//div[@class='line-item-title']")))]
    quantities =  [my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver,"//div[@class='table shorten hide-mobile']/div//div[@class='line-item-bold available']")))]
    for i,j)
    
  • 控制台输出:

    Surgical & Reusable Masks  376,363 available
    Disposable Gloves  66,093 available
    Gowns and Coveralls  40,145 available
    Respirators  22,273 available
    Surface Wipes  20,831 available
    Face Shields  16,686 available
    Hand Sanitizer  11,890 L available
    Thermometers  8,993 available
    Testing Kits  2,815 available
    Surface Solutions  107,452 L available
    Protective Barriers  10,833 available
    Ventilators  410 available
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

您可以在How to retrieve the text of a WebElement using Selenium - Python

中找到相关的讨论

Outro

链接到有用的文档:

,

您必须使用相对xpath来找到带有class="line-item-left"的元素作为每个项目的名称,并找到带有class="line-item-right"的元素作为可用项目的数量。

driver.find_elements_by_class_name("line-item-left") //Item names
driver.find_elements_by_class_name("line-item-right") //Number of items available

请注意元素 s

中的“ s” ,

这是 product name 的选择器:

div.critical-product-table-container div.line-item-left

对于 total

div.critical-product-table-container div.line-item-right

但是下面的方法没有BeautifulSoup

time.sleep(...)是错误的做法,请改用WebDriverWait

并结合上述两个变量并执行并行循环,我尝试使用zip()函数:

url = 'https://www.rrpcanada.org/#/' # site I'm scraping
driver.get(url)
wait = WebDriverWait(driver,150)
product_names = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'div.critical-product-table-container div.line-item-left')))
totals = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'div.critical-product-table-container div.line-item-right')))

for product_name,total in zip(product_names,totals):
    print(product_name.text +'--' +total.text)
    
driver.quit()

您需要进行以下导入:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-