2017-07-01 52 views
1

因爲工作原因,我在使用「driver = webdriver.PhantomJS()」之前能夠刮掉以下網站。我所刮的是價格和日期。如何在刮網站時繞過免責聲明

https://www.cash.ch/fonds/swisscanto-ast-avant-bvg-portfolio-45-p-19225268/swc/chf

這停止工作前幾天,由於我有起初同意免責聲明頁面。

https://www.cash.ch/fonds-investor-disclaimer?redirect=fonds/swisscanto-ast-avant-bvg-portfolio-45-p-19225268/swc/chf

一旦同意我看到視覺的真實內容,但司機好像不是,打印出來是[],所以它必須與聲明的網址是仍。

請參閱下面的代碼。

from selenium import webdriver 
    from bs4 import BeautifulSoup 
    import csv 
    import os 

    driver = webdriver.PhantomJS() 
    driver.set_window_size(1120, 550) 

    #Swisscanto 
    driver.get("https://www.cash.ch/fonds/swisscanto-ast-avant-bvg-  portfolio-45-p-19225268/swc/chf") 
    s_swisscanto = BeautifulSoup(driver.page_source, 'lxml') 
    nav_sc = s_swisscanto.find_all('span', {"data-field-entry": "value"}) 
    date_sc = s_swisscanto.find_all('span', {"data-field-entry": "datetime"}) 

    print(nav_sc) 
    print(date_sc) 
    print("Done Swisscanton") 
+1

試圖找出是否免責聲明設置任何cookie,並做這之前刮。你應該檢查你是否遵守免責聲明,因爲它是有原因的 – Marged

+0

嗨Marget,你可以進一步解釋如何檢查是否免責聲明設置任何cookie?謝謝。 – Shanshan

+0

請看看我的代碼,真的很短,只是添加到帖子。 – Shanshan

回答

2

這應該工作(我想你想在zustimmen點擊按鈕?)這裏

driver = webdriver.PhantomJS() 
driver.get("https://www.cash.ch/fonds/swisscanto-ast-avant-bvg-portfolio-45-p-19225268/swc/chf" 

accept_button = driver.find_element_by_link_text('zustimmen') 
accept_button.click() 

content = driver.page_source 

更多細節 python selenium click on button

+0

嗨,非常感謝!這種情況很奇怪,雖然......一旦點擊「zustimmen」後,瀏覽器就會記住這一點,並且再也沒有問過。瀏覽器顯示包含價格和日期的頁面,但報廢頁面仍然是免責聲明的頁面。如果報廢價格和日期我得到[]打印出來...意思我仍然沒有得到真正的頁面....你知道爲什麼嗎?以及如何解決這個問題?非常感謝! – Shanshan

+0

嗨,我有點卡在那裏。你能如此善良地運行這個簡短的代碼嗎?一旦同意,顯示真實的網站。但是我的打印輸出爲空。 – Shanshan

+0

請看看我的代碼,真的很短,只是添加到帖子中。 – Shanshan