社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

使用selenium和python按类查找元素时,如何仅从<p>和<h2>标记获取文本?

Jerrica • 3 年前 • 1318 次点击  

我试图只从h2和第一个p标签中获取文本。我一直在使用类名来查找div,输出给我div中的所有文本(显然)。

以下是HTML:

<div class="horoscope-content">
<h2> Today's Libra Horoscope for January 27, 2022 <span class="today-badge">TODAY</span></h2>
<p>Go with the flow, Libra. If you find that a situation isn't unfolding the way you'd like it to, take it as a sign to back off. Swimming upstream is hard work, so use your energy more efficiently by exploring different options. When you step back from a stressful situation, circumstances could turn around. Lighten up by considering other possibilities or talking it through with a helpful friend.</p>            
<p>What's in the stars for you tomorrow? <a href="/horoscopes/daily/libra/friday">Read it now</a>.</p>
<div class="dropdown-inline">Read the <b>daily horoscope</b> for another zodiac sign:<div id="dropdown_below_horoscope_dropdown" class="dropdown">

以下是我使用的代码:

libra_content = driver.find_elements(By.CLASS_NAME, 'horoscope-content')

我假设答案是使用xpath,但我不知道如何同时包含这两个标记。我需要使用两行独立的代码来完成这项工作,还是可以将这两行代码合并为一行?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132822
 
1318 次点击  
文章 [ 5 ]  |  最新文章 3 年前
arundeep chohan
Reply   •   1 楼
arundeep chohan    3 年前
libra_content = [[x.find_element(By.XPATH,'./h2[1]').text,x.find_element(By.XPATH,'./p[1]').text] for x in driver.find_elements(By.CLASS_NAME, 'horoscope-content')]

如果想同时存储这两个值,可以对这两个值执行类似的操作。

sanjay oraon
Reply   •   2 楼
sanjay oraon    3 年前

试试这个

<div>
    <h2 class="horoscope-content" >........</h2>
    <p class="horoscope-content" >........</p>            
    <p>.......</p>

libra_内容=驱动程序。查找元素(按.CLASS_名称“占星术内容”)

Jerrica
Reply   •   3 楼
Jerrica    3 年前

我用css选择器解决了这个问题,但没有将它们组合成一个。另一位评论者的答案是使用xpath和类名将两者结合起来,这是一个可能的解决方案。

libra_h2 = driver.find_element(By.CSS_SELECTOR, 'div.horoscope-content > h2')
libra_p = driver.find_element(By.CSS_SELECTOR, 'div.horoscope-content > p')
Muhammad Farooq
Reply   •   4 楼
Muhammad Farooq    3 年前

你可以使用:

对于h2:

libra_content = driver.find_element_by_css_selector("div[class='horoscope-content'] > h2 ")

对于p:

libra_content = driver.find_element_by_css_selector("div[class='horoscope-content'] > p ")

M.Mevlevi
Reply   •   5 楼
M.Mevlevi    3 年前

你可以使用:

libra_content = driver.find_elements(By.xpath, 'your_path')

读一下:

how to find elements by xpath