2017-07-01 51 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