使Selenium不等到页面满载

时间:2016-12-05 10:28:14

标签: python selenium

我尝试将selenium用于chromedriver这样的网页: http://shironet.mako.co.il/artist?type=lyrics&lang=1&prfid=202&wrkid=2473

问题是Selenium总是等到页面加载完毕(例如那里的youtube播放器)。 我只对html来源感兴趣,所以我不想等这么久。 如何让我的程序不要等待?我正在使用python。 (我使用selenium导致urllib没有为此网站工作)

1 个答案:

答案 0 :(得分:0)

几乎没有可能的解决方案:

1)由于您没有澄清您的意思urllib didnt work for this website,您可以尝试使用python-requests库:

pip install requests / cmd

使用Terminal
url = "http://shironet.mako.co.il/artist?type=lyrics&lang=1&prfid=202&wrkid=2473"
page_source = requests.get(url).content

2)尝试使用Firefox Preferences

禁用媒体文件自动播放
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

profile.set_preference("media.autoplay.enabled", False);
driver = webdriver.Firefox(profile)

3)更粗鲁的方法是禁用页面上的Javascript(我不确定你是否真的需要这个用于描述目的)

from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

profile.set_preference("javascript.enabled", False);
driver = webdriver.Firefox(profile)

但要小心,因为它可以从页面源中删除一些必需的媒体文件