如何在滚动添加div的网页上使用Python中的Selenium抓取数据?

我正在尝试从以下网页抓取数据:https://skiplagged.com/flights/YTO/DXB/2020-08-21

我要定位的元素如下:div[@class='infinite-trip-list']//div[@class='span1 trip-duration']

这是一个用户滚动时动态添加元素的列表。我的目标是将这些元素存储在变量中,以提取每次飞行的持续时间。到目前为止,我还无法做到这一点,而这是我在阅读有关此类问题的几篇Stackoverflow帖子后尝试的。

mylist = []

last = driver.execute_script("return document.body.scrollHeight")
while True:
    driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
    time.sleep(1) #let the page load
    new = driver.execute_script("return document.body.scrollHeight")
    infinite_list = driver.find_elements_by_xpath("//div[@class='infinite-trip-list']//div[@class='span1 trip-duration']")
    for elem in infinite_list:
        if elem not in mylist:
            mylist.append(elem.text)
    if new == last: #if new height is equal to last height then we have reached the end of the page so end the while loop
        break
    last = new #changing last's value to new

这将页面滚动到底部,结果我只能看到最后10个值。 我无法编写一段可能会滚动并仅添加正在添加的新div(元素)的代码

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...