python selenium 数据提取

km0tfn4u  于 2023-02-04  发布在  Python
关注(0)|答案(1)|浏览(155)

我在做一个项目

py
i=0
while i<=3:
    i=i+1
    try:
        
        a=driver.find_element(By.XPATH,'//*[@id="search"]/div[1]/div[1]/div/span[1]/div[1]/div[ İ value  ]/div/div/div/div/div[2]/div[1]/h2/a/span').text
        
    except:
        
        a=driver.find_element(By.XPATH,'//*[@id="search"]/div[1]/div[1]/div/span[1]/div[1]/div[ İ value  ]/div/div/div/div/div[3]/div[1]/h2/a/span').text
    print(a)

我想这样做,但是我不能在应该写的地方写i值我正在做一个项目

i=0
while i<=3:
    i=i+1
    try:
        c=str(i)
        veri=(f'//*[@id="search"]/div[1]/div[1]/div/span[1]/div[1]/div[{c}]/div/div/div/div/div[2]/div[1]/h2/a/span')
        a=driver.find_element(By.XPATH,veri).text
        
    except:
        c=str(i)
        veri=(f'//*[@id="search"]/div[1]/div[1]/div/span[1]/div[1]/div[{c}]/div/div/div/div/div[3]/div[1]/h2/a/span')
        a=driver.find_element(By.XPATH,veri).text
    print(a)

我试过这个bandy,但它没有发生,我知道如何做,任何人都可以与它的知识帮助

eqqqjvef

eqqqjvef1#

使用f字符串

a=driver.find_element(By.XPATH,f'//[@id="search"]/div[1]/div[1]/div/span[1]/div[1]/div[{i}]/div/div/div/div/div[2]/div[1]/h2/a/span').text

或旧样式字符串格式

a=driver.find_element(By.XPATH,'//[@id="search"]/div[1]/div[1]/div/span[1]/div[1]/div[%d]/div/div/div/div/div[2]/div[1]/h2/a/span' % i).text

相关问题