简体   繁体   English

如何在python中找到最大xpath值

[英]How to find the max xpath value in python

I'm working on scraping data in Selenium using xpaths.我正在使用 xpaths 在 Selenium 中抓取数据。 I want to iterate across xpaths with differing numeric values.我想遍历具有不同数值的 xpath。 For example, I want to iterate across the following xpath: "//*[@id="content-core"]/div/p[i]" with i being the values 1 to n .例如,我想遍历以下 xpath: "//*[@id="content-core"]/div/p[i]"其中i是值1n

Is there a function or code to find n , the maximum number in a given Xpath equation, so I know when to stop the loop?是否有函数或代码可以找到给定 Xpath 方程中的最大数n ,以便我知道何时停止循环?

Thanks so much!非常感谢!

You can use a try-except block, like this:您可以使用try-except块,如下所示:

from selenium.common.exceptions import NoSuchElementException

i = 1

while True:
    try:
        xpath = f"//*[@id="content-core"]/div/p[{i}]"
        driver.find_element_by_xpath(xpath)
        i += 1

    except NoSuchElementException: 
        break

You can use count to solve it.您可以使用count来解决它。 For example, assume if you have extract an html of an url , as follows:例如,假设您提取了urlhtml ,如下所示:

import requests
from lxml import etree

response = requests.get(url)
data = response.text
html = etree.HTML(data)

Then, you can get the length of the label p .然后,您可以获得标签p的长度。

len = int(html.xpath('count(//*[@id="content-core"]/div//p)'))

Find the elements len and then loop through them.找到元素 len 然后遍历它们。

n=len(driver.find_elements_by_xpath("//*[@id='content-core']/div/p"))
for i in range(n):
  try:
    driver.find_element_by_xpath("//*[@id='content-core']/div/p["+i+"]")
  except:
    break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM