【问题标题】:Selenium infinite srolling and finding end loopSelenium 无限滚动和查找结束循环
【发布时间】:2022-01-23 23:29:44
【问题描述】:

我正在尝试废弃 Magic Eden,特别是我想要获取页面中存在的所有集合的页面集合。我想我已经完成了一半,但我无法弄清楚下面的两个问题。

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

url = "https://magiceden.io/collections"

chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome("../chromedriver/chromedriver")
# driver = webdriver.Chrome("../chromedriver/chromedriver",chrome_options=chrome_op tions)
driver.get(url)

## There is a catch with the site, it loads more data when you scroll down so I need to add this function part to scroll until the end

while driver.find_element_by_tag_name('div'):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    Divs=driver.find_element_by_tag_name('div').text
    if 'End of Results' in Divs:
        print('end')
        break
    else:
        continue

"""
Q1 : I need to find a way to break out the while loop. I don't seem to find a pattern in the end of the page
"""

"""
Q2 : How do I get after loading the page, all of the href and the names of each project ?
"""

【问题讨论】:

  • 网页无限滚动,你想在什么情况下突破?
  • 项目数量有限。如果你运行我的代码行并检查页面,你会看到你走到了页面的末尾,但是循环仍在继续,因为我不知道如何找到一个好的模式来打破它。跨度>

标签: python selenium web-scraping


【解决方案1】:

如果页面有一个结尾并一次加载所有内容,而不是使用 while 循环并向下滚动页面,我将只使用一个

Divs=driver.find_elements_by_tag_name('div').text

Divs=driver.find_element_by_tag_name('div').text

不同之处在于元素会查找页面上的所有元素并将其作为列表返回。另一方面,元素只会返回第一个。这应该可以解决您不知道何时停止的问题,因为它会抓住您想要的每个元素。我会说,如果您尝试针对特定类型的元素,我会使用更精确的元素,例如

Divs=driver.find_elements_by_class("classname").text

这样你就拥有了你想要的所有元素,而不是页面上的每个 div。 最后,只需使用类似

for element in list:
    element.getAttribute("href");

这会让你得到你想要的。

【讨论】:

  • 感谢您的回复,但是 find_elements_by_class 不存在,您的意思是 find_element_by_class_name 吗?如果这就是你的意思,我尝试了这个 driver.find_element_by_class_name("p-0 text-center card collection-card me") 但是我得到一个错误 => driver.find_element_by_class_name("p-0 text-center card collection-card我”)
  • 对不起,我的意思是放“find_elements_by_class_name”,而不是“find_elements_by_class” 我认为现在的问题是 find_elements_by_class_name 只能接受一个类名。例如 driver.find_element_by_class_name("p-0") 您目前正在给它 4 个不同的类名,我相信。如果其中一个是您正在寻找的元素所独有的,请使用它。如果不是,我会通过它的 xpath 找到元素。
【解决方案2】:

我找到了解决问题的方法。我的解决方案有两个重要部分:

  1. 使用时间睡眠等待页面加载
  2. 根据内容的高度选择何时停止滚动

我希望它可以帮助其他人。谢谢大家的贡献。

def getCollectionUrl(sleepSec:int):
    """
    
    """
    url = "https://magiceden.io/collections"
    driver = webdriver.Chrome("../chromedriver/chromedriver")
    driver.get(url)
    time.sleep(sleepSec)
    prev_height = driver.execute_script('return document.body.scrollHeight')
    ### This is the part that you really need         
    while True:
        driver.execute_script('window.scrollTo(0,document.body.scrollHeight);')
        time.sleep(sleepSec)
        new_height = driver.execute_script('return document.body.scrollHeight')
        if new_height == prev_height:
            break
        prev_height = new_height


    locContent = driver.page_source.split("p-0 text-center card collection-card me")
    locListUrl = []
    for c_ in locContent[1:]:
        locListUrl.append("https://magiceden.io"+c_.split('href="')[1].split('"><div class=')[0])
    driver.quit()
    return locListUrl

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-30
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 2017-01-17
    • 1970-01-01
    • 2015-09-05
    相关资源
    最近更新 更多