将Selenium与Python结合使用,如何单击“查看更多”以在页面中加载更多项目?

问题描述 投票:0回答:1

我在学习Python时非常陌生,因此这是一个基本而简单的事情。我已经做了很多研究,并根据提供的解决其他问题的方法尝试了各种方法,但我陷入了困境。我正在尝试从不同的URL抓取数据(对于此特定示例,https://www.dtlr.com/collections/men-footwear),但是当我向下滚动页面时,有一个按钮显示“查看更多”。正如您在脚本中看到的那样,我尝试了各种方法来单击此按钮,然后继续执行脚本行以滚动浏览新列出的项目,但是似乎没有任何效果。我发现的一篇帖子提到必须在视图中单击需要单击的按钮。我什至尝试使用elem.send_keys(Keys.PAGE_UP)向上滚动,甚至在视图中单击该按钮也从未起作用。有指导吗?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import re
import math
import random
import time

shoe_sites = ["https://www.dtlr.com/collections/men-footwear",
              "https://www.dtlr.com/collections/women-footwear",
              "https://www.dtlr.com/collections/kids-age-group-grade-school",
              "https://www.dtlr.com/collections/kids-age-group-pre-school",
              "https://www.dtlr.com/collections/kids-age-group-toddler-infant"]

for x in shoe_sites:
    my_url = x
    browser = webdriver.Chrome()
    browser.get(my_url)
    browser.maximize_window()
    time.sleep(random.randint(1,5))

    elem = browser.find_element_by_tag_name("body")

    no_of_pagedowns = 50

    while no_of_pagedowns:
        elem.send_keys(Keys.PAGE_DOWN)
        try:
            browser.find_element_by_css_selector('#ltkpopup-close-button > a').click()
        except Exception:
            try:
                browser.find_element_by_css_selector('#Collection > div > ul > a').click()
            except Exception:
                try:
                    browser.find_element_by_xpath('//*[@id="Collection"]/div/ul/a').click()
                except Exception:
                    try:
                        browser.find_elements_by_class_name('loadmore btn').click()
                    except Exception:
                        try:
                            element = browser.find_elements_by_class_name('loadmore btn')
                            coordinates = element.location_once_scrolled_into_view
                            browser.execute_script('window.scrollTo({}, {});'.format(coordinates['x'], coordinates['y']))
                            element.click()
                        except Exception:
                            test = 1

        time.sleep(random.randint(1,5))
        no_of_pagedowns-=1

    post_elems = browser.find_elements_by_class_name("product_men")
    html = browser.page_source
    browser.close()
python selenium web-scraping click infinite-scroll
1个回答
0
投票

要找到您要表示的See More按钮,请使用<< [.find_element_by_css_selector("a.loadmore.btn")] >>

© www.soinside.com 2019 - 2024. All rights reserved.