from selenium import webdriver
from selenium.webdriver.common.by import By
try:
    url = "https://www.imdb.com/chart/top/"
    driver = webdriver.Chrome()
    driver.get(url)
    title = driver.find_elements(By.XPATH,"//*[(@class = 'ipc-title ipc-title--base ipc-title--title ipc-title-link-no-icon ipc-title--on-textPrimary sc-43986a27-9 gaoUku cli-title')]/a/h3")
    year = driver.find_elements(By.XPATH,"//*[(@class = 'sc-43986a27-8 jHYIIK cli-title-metadata-item')][1]")
    
    for i,j in zip(title,year):
        print(i.get_attribute("innerHTML"))
        print(j.get_attribute("innerHTML"))
except Exception as e:
    print(e)

我只想知道有没有其他方法可以在forloop中使用多个函数,而不使用ZIP逗号.

谢谢.

推荐答案

您可以使用INDEX遍历这两者

for i in range(len(title)):
    print(title[i].get_attribute("innerHTML"))
    print(year[i].get_attribute("innerHTML"))

然而,我认为您应该找到一个共同的父级,并使用它来定位您正在寻找的元素

items = driver.find_elements(By.CLASS_NAME, 'ipc-metadata-list-summary-item__c')

for item in items:
    print(item.find_element(By.CLASS_NAME, 'ipc-title__text').get_attribute('innerHTML'))
    print(item.find_element(By.CLASS_NAME, 'cli-title-metadata-item').get_attribute('innerHTML'))

输出

1. The Shawshank Redemption
1994
2. The Godfather
1972
3. The Dark Knight
2008
4. The Godfather Part II
1974
5. 12 Angry Men
1957
...

Python-3.x相关问答推荐

使用PANAS根据另两个列表中的值对一个列表中的字符串值进行分组

Pyvis和Networkx:如何根据源或目标使 node colored颜色 不同

新行是pandas数据帧中旧行的组合

为什么我无法在django中按月筛选事件?

如何强调您正在寻求以 pandas 数据帧的另一列为条件的差异?

如何立即从asyncio.Task获取异常?

TypeError: issubclass() arg 1 在 Flask 中导入 langchain 时必须是一个类

Python 列表求和所有出现的保留顺序

有没有办法使用 python opencv 计算与图像的白色距离

Pythonic,自定义警告

pythondecorator中的变量范围

如何使用python将放置在多个嵌套文件夹中的文档移动和重命名为一个新的单个文件夹?

如何在多核上运行 Keras?

tkinter TclError:错误的文件类型使用 askopenfilename

当 None 被传递时,如何将默认值应用于 python 数据类字段?

谁能给我一个 Python 3 中标准输入和标准输出的快速教程?

计算两个文件的行差异的最有效方法是什么?

三个参数的reduce函数

python asyncio add_done_callback 与 async def

TypeError:无法将系列转换为