【问题标题】:Get data after click with Selenium (Python 3)使用 Selenium(Python 3)点击后获取数据
【发布时间】:2020-12-15 12:37:42
【问题描述】:

我正在尝试用简单的信息抓取一个页面。

我正在使用 BeautifulSoup 来抓取数据。但是在页面中有一个隐藏电子邮件信息的按钮。因此,我尝试使用 Selenium 来小鸡,然后使用 BeautifulSoup 抓取数据。但我真的不知道该怎么做。

我做到了:

import requests, time, re
from bs4 import BeautifulSoup
from selenium import webdriver

url = "https://acukwik.com/Basic-Info/UUBP/RUSAERO"

driver = webdriver.Chrome()
driver.get(url)

while True:
    soup = BeautifulSoup(driver.page_source, 'html5lib')
    divEmail = soup.find('div', text=re.compile('Email'))
    try:
        driver.find_elements_by_class_name('ghEmail').click()
        time.sleep(3)
        email = divEmail.findNext('a')['href']
        print(email)
    except:
        break

driver.quit()

发生的情况是 Chrome 页面在给定的 url 中打开,但没有任何反应。它只是打开和关闭。我没有看到按钮发生变化。

我做错了什么?以及如何获取这些数据?我可以只用 BeautifulSoup 吗?

【问题讨论】:

标签: python python-3.x selenium web-scraping beautifulsoup


【解决方案1】:

在点击show mail 按钮之前,您正在喝汤。需要先点击按钮,然后获取页面源代码。

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
import time, re
from bs4 import BeautifulSoup

driver=webdriver.Chrome()
driver.get("https://acukwik.com/Basic-Info/UUBP/RUSAERO")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.ghEmail"))).click()
time.sleep(1)
soup = BeautifulSoup(driver.page_source, 'html5lib')
divEmail = soup.find('div', text=re.compile('Email'))
email = divEmail.findNext('a')['href']
print(email)

【讨论】:

    【解决方案2】:

    ypou 正在使用返回 alist 的 find_elements,并且您尝试单击列表,因此它总是失败,因此您可以将 find_elements_by_class 更改为 find_element_by_class。

    您也可以使用定位器:"//div[contains(text(),'Email')]/../div[2]/a"

    点击后收到邮件

    from selenium import webdriver
    import time
    
    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("window-size=1920,1080")
    #chrome_options.add_argument("--no-sandbox") # linux only
    chrome_options.add_argument("--headless")
    
    url = "https://acukwik.com/Basic-Info/UUBP/RUSAERO"
    
    driver = webdriver.Chrome(options=chrome_options)
    driver.get(url)
    divEmail = driver.find_element_by_class_name('ghEmail').click()
    
    time.sleep(1)
    
    email = driver.find_element_by_xpath(
        "//div[contains(text(),'Email')]/../div[2]/a")
    print(email.text)
    driver.quit()
    

    【讨论】:

    • 谢谢。这正是我需要的!一个小问题:如何更改为不弹出 chrome 页面?
    • 使用 options.add_arguments 无头
    【解决方案3】:

    试试这个代码来获取电子邮件:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    ...
    driver.find_element_by_class_name('ghEmail').click()
    email = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//a[starts-with(@href, "mailto:")]'))).text
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-05
      • 2018-07-25
      • 2015-03-26
      • 2021-03-16
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多