【问题标题】:Scraping dictionary.cambridge.org using Python Selenium使用 Python Selenium 抓取 dictionary.cambridge.org
【发布时间】:2018-04-22 23:49:02
【问题描述】:

我想获得从 dictionary.cambridge.org 下载 mp3 文件的链接。 xpath 已找到正确的按钮,但在任何情况下我都无法获得链接。我试图使用 .text.get_attribute("href") 方法。你有什么主意吗 ?

from selenium import webdriver

words=['hunch']
link='https://dictionary.cambridge.org/dictionary/english-polish'

driver = webdriver.Chrome()

main_window = driver.current_window_handle
for i in words:
    driver.get(link+"/"+str(i))
    try:
        content = driver.find_elements_by_xpath('//*[@id="entryContent"]/div[3]/div/div/div[1]/span/span[2]/span[1]/span[2]')
        print(content)
      # print(content.text)
    except:
        driver.close()
    driver.close()

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping selenium-chromedriver


    【解决方案1】:

    根据url,要从 data-src-mp3 属性中检索链接,您需要诱导 WebDriverWait 并且可以使用以下代码行:

    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
    # lines of code
    content = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='circle circle-btn sound audio_play_button uk']")))
    print(content.get_attribute("data-src-mp3"))
    

    控制台输出:

    https://dictionary.cambridge.org/media/english-polish/uk_pron/u/ukh/ukhun/ukhunch001.mp3
    

    【讨论】:

    • 完成。正如我所见,无法使用 Selenium 下载该文件。
    • IMO,可以使用 Selenium 下载该文件,但您可以考虑为这个问题提出新票。
    猜你喜欢
    • 2020-01-10
    • 1970-01-01
    • 2020-10-09
    • 2019-10-30
    • 2020-12-29
    • 2020-11-15
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多