简体   繁体   English

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

[英]Using Selenium with Python, how do I click “See More” to load more items in a page?

I preface this question with I am VERY new to learning Python, so this may be something basic and simple.我以我对学习 Python 非常陌生,所以这可能是一些基本而简单的问题。 I've done a lot of research and tried various approaches based on what has been provided as potential solutions to other questions, and I am stuck.我做了很多研究,并根据已提供的其他问题的潜在解决方案尝试了各种方法,但我被困住了。 I am trying to scrape data from different urls (for this specific example, https://www.dtlr.com/collections/men-footwear ), but when I scroll down the page there is a button that says "See More".我正在尝试从不同的 url 中抓取数据(对于这个特定的示例, https://www.dtlr.com/collections/men-footwear ),但是当我向下滚动页面时,有一个显示“查看更多”的按钮。 As you can see in my script, I've tried various ways to click this button and continue the line of script to scroll through the newly listed items, but nothing seems to work.正如您在我的脚本中看到的那样,我尝试了各种方法来单击此按钮并继续脚本行以滚动浏览新列出的项目,但似乎没有任何效果。 One post that I found mentioned that the button that needs to be clicked must be in view.我发现的一篇文章提到需要单击的按钮必须在视图中。 I even tried scrolling back up using elem.send_keys(Keys.PAGE_UP) , and even with the button in view the clicking never worked.我什至尝试使用elem.send_keys(Keys.PAGE_UP)向上滚动,即使使用视图中的按钮,点击也不起作用。 Any guidance?有什么指导吗?

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()

To locate See More button you mean, use .find_element_by_css_selector("a.loadmore.btn")要找到您的意思的See More按钮,请使用.find_element_by_css_selector("a.loadmore.btn")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM