【Python】让Selenium 像Beautifulsoup一样,用解析HTML 结构的方式提取元素!

我在使用selenium的find_element的方式去获取网页元素,一般通过xpath、css_selector、class_name的方式去获取元素的绝对位置。

但是有时候如果网页多了一些弹窗或者啥之类的,绝对位置会发生变化,使用xpath等方法,需要经常变动。

于是我在想,能不能让selenium也能像Beautifulsoup一样,可以根据html的结构找到需要的部分,并解析出来。

方法:

  1. 复制那里的css_selector
  2. 对比css_selector的构建和html上的元素的上下位置
python 复制代码
products=page_soup.find('div', {'id': 'List'}).ul.findAll("li") #找到最大的那个位置
for product in products:
    # 提取商品链接
    link_element = product.find_element(By.CSS_SELECTOR, "p-name  a")
        product_link = link_element.get_attribute("href")
        product_title = link_element.get_attribute("title")

写法类似beautifulsoup的写法。

  1. 如果想要多个条件并列,写法:
python 复制代码
   tags_elements = product.find_elements(By.CSS_SELECTOR, "div.p-icons img, div.p-icons i")

这个是想同时获得icons 下的img 和i 的节点元素的内容。

  1. 提取上一级或者下一级的写法:

例如:提取 div 的p-icons的,下一级元素img;

css_selector : #J_pro_100151669791 > img:nth-child(1)

在div class为"p-icons"下的

具体写法:

python 复制代码
 tags_elements = product.find_elements(By.CSS_SELECTOR, "div.p-icons  img:nth-child(1)")
  1. 提取其中的具体标签值,例如 像上面的desc的:
python 复制代码
for tag_element in tags_elements:
    tag = tag_element.get_attribute("desc") or tag_element.text
    if "XX超市" in tag or "五星旗舰店" in tag or "自营" in tag:
        tags.append(tag.strip())

可以批量判断是否为这个标签值


总的写法:

python 复制代码
for product in products:
    print()
    # 提取商品链接 
    link_element = product.find_element(By.CSS_SELECTOR, "div.p-name a")
    #print('提取商品链接:',link_element)

    #产品链接 产品名称
    product_link = link_element.get_attribute("href") #产品链接
    product_title = link_element.text #产品名称
    print(product_title)
    print('提取商品链接:',product_link)

    #价格     
    product_price_element = product.find_element(By.CSS_SELECTOR, "div.p-price i")
    product_price = product_price_element.text if product_price_element else "无"
    print(product_price)

    #评价数 #warecard_10116099611938 > div.p-commit > strong
    comment_count_element = product.find_element(By.CSS_SELECTOR, "div.p-commit a")
    comment_count = comment_count_element.text if comment_count_element else "无"
    print(comment_count)
    
    # 提取店铺名称
    shop_name_element = product.find_element(By.CSS_SELECTOR, "div.p-shop a, div.p-shop span")  ##warecard_10129282745285 > div.p-shop > span
    shop_name = shop_name_element.text if shop_name_element else "无"
    print(shop_name)


    #划线价
    original_price= is_exist_element(product,"div.p-price span.originalPrice")
    print(original_price)
    
   
    #自营
    is_self_operated = is_extact_element_element(product,"div.p-name.p-name-type-2 img","alt","自营")
    print(is_self_operated)
    

    #X东超市
    is_jd_supermarket = is_extact_element_element(product, "div.p-icons img","desc",'XX超市')
    print(is_jd_supermarket)
    
    #5星店铺  
    is_five_star = is_element(product,"div.p-shop img")
    print(is_five_star)
相关推荐
都叫我大帅哥13 分钟前
深度学习中的"火眼金睛":卷积神经网络(CNN)终极指南
python·深度学习
都叫我大帅哥16 分钟前
幽默深度指南:LangChain中的RunnableParallel - 让AI任务像交响乐团般协同工作
python·langchain·ai编程
liliangcsdn28 分钟前
python 12 install jupyter时zmq.h或libzmq报错处理
ide·python·jupyter
_Orch1d29 分钟前
初识无监督学习-聚类算法中的K-Means算法,从原理公式到简单代码实现再到算法优化
python·学习·算法·机器学习·numpy·kmeans·聚类
青衫客361 小时前
Python 实例属性与方法命名冲突:一次隐藏的Bug引发的思考
python
人邮异步社区1 小时前
先学Python还是c++?
开发语言·c++·python
旧时光巷3 小时前
【机器学习③】 | CNN篇
人工智能·pytorch·python·机器学习·cnn·卷积神经网络·lenet-5
amazinging3 小时前
北京-4年功能测试2年空窗-报培训班学测开-第六十六天
python·学习·面试
叫我:松哥8 小时前
python案例:基于python 神经网络cnn和LDA主题分析的旅游景点满意度分析
人工智能·python·神经网络·数据挖掘·数据分析·cnn·课程设计
2202_756749699 小时前
01 基于sklearn的机械学习-机械学习的分类、sklearn的安装、sklearn数据集及数据集的划分、特征工程(特征提取与无量纲化、特征降维)
人工智能·python·机器学习·分类·sklearn