【问题标题】:How to get the whole source code from a web page with selenium / webdriver?如何使用 selenium / webdriver 从网页获取整个源代码?
【发布时间】:2021-03-18 22:56:41
【问题描述】:

我(通常)成功地使用这个 python 程序进行网页抓取。 它不仅给了我页面的源代码,还给了我隐藏在 Javascript 后面的代码。 但是,它在此特定网站上无法正常工作。信息丢失。 这似乎不是时间问题。

from selenium import webdriver

url = "https://www.youbet.dk/sport/fodbold/"

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(executable_path='D:/Programme/chromedriver_win32/chromedriver.exe',options=options)
driver.get(url)

执行后,driver.page_source 包含代码。

我对按钮上的文字(团队名称和号码)感兴趣。 右键单击并检查 Chrome 中的按钮会给我类似于以下代码的内容,其中包含我正在寻找的信息(此处为“Villarreal”和“1.51”):

<button class="rj-ev-list__bet-btn  rj-ev-list__selection-0ML54283820_1" data-uat="button-ev-list-bet-btn"><div class="rj-ev-list__bet-btn__inner " data-uat="div-ev-list-bet-btn-inner"><div class="rj-ev-list__bet-btn__row" data-uat="div-ev-list-bet-btn-row"><span class="rj-ev-list__bet-btn__content rj-ev-list__bet-btn__text" data-uat="ev-list-ev-list-bet-btn-text">Villarreal</span></div><div class="rj-ev-list__bet-btn__row" data-uat="div-ev-list-bet-btn-row"><span class="rj-ev-list__bet-btn__content rj-ev-list__bet-btn__odd" data-uat="ev-list-ev-list-bet-btn-odd">1.51</span></div></div><span class="rj-ev-list__bet-btn__arrow-up"></span><span class="rj-ev-list__bet-btn__arrow-down"></span></button>

但这并没有显示在 driver.page_source 中。

如何使用 python 和 selenium 访问这些信息?

这些没有帮助:

* Adding time.sleep(10)
* Adding driver.implicitly_wait(10)

【问题讨论】:

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


    【解决方案1】:
    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
    from bs4 import BeautifulSoup
    
    url = "https://www.youbet.dk/sport/fodbold/"
    
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    
    driver = webdriver.Chrome(options=options)
    driver.get(url)
    WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'rj-ev-list__bet-btn__inner')))
    html = driver.page_source
    soup = BeautifulSoup(html, 'html.parser')
    mydivs = soup.find_all("button", {"class": "rj-ev-list__bet-btn"})
    alldata = [[div.find("span", {"class": "rj-ev-list__bet-btn__content"}).text,
                div.find("span", {"class": "rj-ev-list__bet-btn__odd"}).text] for div in mydivs]
    print(alldata)
    driver.quit()
    # [['Fulham', '2.55'], ['Uafgjort', '3.40'], ['Leeds', '2.80'], ['Real Betis', '1.83'], ['Uafgjort', '3.65'], ['Levante', '4.55'], ['Parma', '2.40'], ['Uafgjort', '3.10'], ['Genoa', '3.35']]
    

    您的方法的问题: 你很亲密。您添加到代码中的延迟问题在于它们与元素的可见性没有直接关系(可能十秒的等待时间还不够)。为了解决这个问题,在这段代码中,我使用了更具体的 WebDriverWait(附加资源:https://www.geeksforgeeks.org/explicit-waits-in-selenium-python/

    WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located(
    (By.CLASS_NAME, 'rj-ev-list__bet-btn__inner')))
    

    等待类中所有元素的存在。代码解决方案对我有用。如果您有任何问题,请在 cmets 中告诉我。

    【讨论】:

    • 非常感谢!这解决了问题,您为我未来的项目提供了宝贵的见解。
    猜你喜欢
    • 2016-05-30
    • 2020-07-31
    • 2015-06-14
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    相关资源
    最近更新 更多