【问题标题】:Click on load more button using selenium not working properly in python3.7单击使用 selenium 的加载更多按钮在 python3.7 中无法正常工作
【发布时间】:2019-06-26 04:21:58
【问题描述】:

当我在抓取时,页面是动态的,带有“加载更多”按钮。 我为此使用了硒。 第一个问题是它只能工作一次。意味着第一次单击加载更多按钮。 第二个问题是它只抓取第一个加载更多按钮之前的文章。之后就不刮了。 第三个问题是它把所有的文章都刮了两次。 第四个问题是我只想要日期,但它与日期、作者和地点一起给出。

import time
import requests
from bs4 import BeautifulSoup
from bs4.element import Tag
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
base = "https://indianexpress.com"
browser = webdriver.Safari(executable_path='/usr/bin/safaridriver')
wait = WebDriverWait(browser, 10)
browser.get('https://indianexpress.com/?s=cybersecurity')

while True:
    try:
        time.sleep(6)
        show_more = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Load More')))
        show_more.click()
    except Exception as e:
            print(e)
            break

soup = BeautifulSoup(browser.page_source,'lxml')
search_results = soup.find('div', {'id':'ie-infinite-scroll'})

links = search_results.find_all('a')
for link in links:
    link_url = link['href']
    response = requests.get(link_url)
    sauce = BeautifulSoup(response.text, 'html.parser')
    dateTag = sauce.find('div', {'class':'m-story-meta__credit'})
    titleTag = sauce.find('h1', {'class':'m-story-header__title'})
    contentTag = ' '.join([item.get_text(strip=True) for item in sauce.select("[class^='o-story-content__main a-wysiwyg'] p")])

    date = None
    title = None
    content = None

    if isinstance(dateTag, Tag):
        date = dateTag.get_text().strip()
    if isinstance(titleTag, Tag):
        title = titleTag.get_text().strip()

    print(f'{date}\n {title}\n {contentTag}\n')
    time.sleep(3)

这段代码没有错误。但它需要细化。如何解决上述问题?

谢谢。

【问题讨论】:

    标签: selenium-webdriver beautifulsoup python-3.7


    【解决方案1】:

    因为您不是在等待新内容。当新内容等待加载时,您正在尝试点击“加载更多”按钮。

    错误信息:

    Message: Element <a class="m-featured-link m-featured-link--centered ie-load-more" href="#"> is not clickable at point (467,417) because another element <div class="o-listing__load-more m-loading"> obscures it
    

    我的解决方案:

    while True:
        try:
            wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@class, 'ie-load-more')]")))
            browser.find_element_by_xpath("//a[contains(@class, 'ie-load-more')]").click()
            wait.until(EC.visibility_of_element_located((By.XPATH,"//div[@class='o-listing__load-more']")))
        except Exception as e:
            print(e)
            break
    

    【讨论】:

    • 谢谢。有效。我接受并赞成这个答案。
    猜你喜欢
    • 2022-01-21
    • 2012-03-06
    • 2021-12-08
    • 2018-09-07
    • 1970-01-01
    • 2022-12-18
    • 2021-09-28
    • 1970-01-01
    • 2014-09-09
    相关资源
    最近更新 更多