有些页面不能直接使用请求获取内容,会动态执行一些js代码生成内容。本文主要是针对那些特殊的页面,比如必须调用js才能下载的情况。安装chrome1.wget[https://dl.google.com/linux/direct/google-chrome-stable\_current\_x86\_64.rpm](https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm)2.yum安装./google-chrome-stable\_current\_x86\_64.rpm3.yum安装mesa-libOSMesa-develgnu-free-sans-fontswqy-zenhei-fonts安装chromedriver1。淘宝源码(推荐)wgethttp://npm.taobao.org/mirrors/chromedriver/2.41/chromedriver_linux64.zip2.unzipchromedriver\_linux64.zip3.movechromedriver/usr/bin/4.chmod+x/usr/bin/chromedriverThanks在本博客的以上步骤中,您可以选择适合自己的版本下载。注意:chrome和chrome驱动必须是匹配的版本,chrome驱动会注明支持的chrome版本号。实战中需要引入的库fromseleniumimportwebdriverfromtimeimportsleepfromselenium.webdriver.chrome.optionsimportOptionsfromselenium.common.exceptionsimportNoSuchElementExceptionchrome启动设置chrome_options=Options()chrome_options.add_argument('--)#solutionsandDevToolsActivePortfiledoesnotexisterrorchrome_options.add_argument('window-size=1920x3000')#指定浏览器分辨率chrome_options.add_argument('--disable-gpu')#谷歌文档提到这个属性需要添加以避免bugchrome_options。add_argument('--hide-scrollbars')#隐藏滚动条,处理一些特殊页面chrome_options.add_argument('blink-settings=imagesEnabled=false')#不加载图片,提高速度chrome_options.add_argument('--headless')#Browser不提供可视化页面。如果linux下系统不支持可视化,不加这个会启动失败。也感谢上面博客设置了额外的参数,比如下载不弹窗和默认下载路径prefs={'profile.default_content_settings.popups':0,'download.default_directory':'./filelist'}chrome_options。add_experimental_option('prefs',prefs)初始化驱动cls.driver=webdriver.Chrome(options=chrome_options)退出驱动cls.driver.quit()请求一个urlcls.driver.get(url)执行cls指定的js代码。driver.execute_script('缺点ole.log("helloworld")')查找指定元素subtitle=cls.driver.find_element_by_class_name("fubiaoti").text
