之前在python环境下selenium 主要用的是chromdriver,此次发现总是报错(Timeout), 实际又是正确的, 多是和chrome版本不正确,再加上我程序蹦来就在windows环境下。javascript
IE浏览器驱动下载连接:http://selenium-release.storage.googleapis.com/index.html,我这里安装的是v3.0(担忧最新的有问题),运行代码发现成功。html
Firefox 浏览器下载连接:https://github.com/mozilla/geckodriver/releases/, 我这里安装的是 java
geckodriver-v0.24.0-win64.zip ,Firefox 是最新的68.0\python
若是是在linux(ubuntu)下, 首先下载文件,而后切换到下载目录 执行如下语句:linux
tar -xvzf geckodriver* chmod +x geckodriver sudo mv geckodriver /usr/local/bin/
能够参考 https://blog.csdn.net/qq_23926575/article/details/77268924git
把下载文件放在python的安装目录的Scripts下比较方便(这样就能够不用指定路径了)github
咱们在爬网的时候常常须要用到requests(发送http请求)和BeautifulSoup 分析网页的返回内容web
csdn:chrome
from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.common.exceptions import TimeoutException import requests from bs4 import BeautifulSoup import time opts = Options() #opts.headless =True opts.add_argument("--headless") #br = webdriver.Ie(r'D:/python/IEDriverServer.exe') url="https://blog.csdn.net/xxx/article/list/" for i in range(60): try: #br = webdriver.Firefox(firefox_options=opts) br = webdriver.Firefox(options=opts) #br = webdriver.Ie(r'D:/python/IEDriverServer.exe') r = requests.get(url+str(i)) soup = BeautifulSoup(r.text,"lxml") s= soup.find_all("div",class_ ="article-item-box csdn-tracking-statistics") for item in s: temp=item.h4.a.get("href") if temp.startswith("https://blog.csdn.net/xxx"): try: print(temp) br.get(temp) time.sleep(60) except TimeoutException: br.execute_script("window.stop()") except Exception as et: print("Error detail:",et) except Exception as e: print("Error:",e) finally: br.quit()
cnblogs:ubuntu
from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.common.exceptions import TimeoutException import requests from bs4 import BeautifulSoup import time opts = Options() opts.add_argument("--headless") url="https://www.cnblogs.com/xxx/default.html?page=" for i in range(9): try: #br = webdriver.Firefox(firefox_options=opts) br = webdriver.Firefox(options=opts) r = requests.get(url+str(i)) soup = BeautifulSoup(r.text,"lxml") soup = soup.find(id='content') s= soup.find_all("h2") for item in s: temp=item.a.get("href") if temp.startswith("https://www.cnblogs.com/xxx/"): try: print(temp) br.get(temp) time.sleep(60) except TimeoutException: br.execute_script("window.stop()") except Exception as et: print("Error detail:",et) except Exception as e: print("Error:",e) finally: br.quit()
而后运行 pyinstaller -F xxx.py来打包成exe文件
在windows用以下语句
opts.headless =True
br = webdriver.Firefox(options=opts)
在ubuntu须要改成
opts.add_argument("--headless")
br = webdriver.Firefox(firefox_options=opts)
若是windows下会有以下提示:
可是在处理https的时候须要注意了:
调用IE来打开对应的网页问题,可是在实际测试中,有些网站是采用https协议的,这时候IE浏览器会弹出以下窗口,通常手动选择后,才可进入登陆界面,而在webdriver调用浏览器后,没法继续操做,那么该如何解决呢?
方法一:代码增长配置
首先,咱们能够能够查看该网页的源码,分析下代码,能够看到下面部分信息:
<h4 id="continueToSite">
<img src="red_shield.png" ID="ImgOverride" border="0" alt="不推荐图标" class="actionIcon">
<A href='' ID="overridelink" NAME="overridelink" >继续浏览此网站(不推荐)。 </A>
</h4>
述标记部分的,则是上图标记的地方,通常咱们点击该图标后便可进入登陆窗口,下面代码中经过调用javascript来操做浏览器的提示框,来跳过该提示便可:
#coding=utf-8 from selenium import webdriver driver=webdriver.Ie() firsturl='https://172.172.110.8/Terminal/logon.do' driver.get(firsturl) driver.get("javascript:document.getElementById('overridelink').click();")#解决IE提示问题 driver.close()
方法二:浏览器配置
方法二则是经过配置浏览器的方法,解决证书问题,方法以下:
一、点击【继续浏览此网页】后进入登陆窗口,此时地址栏后面会出现【证书错误】提示
二、点击证书错误——查看证书,提示证书无效,则是由于证书不被信息,须要安装证书
三、弹出证书界面,选择安装证书
四、按向导操做,注意在下列步骤中须要选择证书位置
五、配置完成后,此时依然是没法登录的,点击继续浏览后,弹出的错误提示为:不匹配的地址,以下,还须要继续配置
六、Internnet选项——高级下,去除下图中标记项的勾,而后保存
七、从新打开地址,此时仍然会弹出提示,选择继续浏览后,会发现上方的地址栏变为一个小锁,如右图,说明已经配置OK,后续在打开该地址就不会弹出错误选项了。
八、若仍是没法登录,能够在Internet选项—安全中:设置安全等级为低等级,并在高级选项下,将下属五项所有勾选后重启浏览器便可
关于调用IE浏览器的错误处理
运行过程当中若是出现错误:WebDriverException: Message: u'Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.
解决方法
更改IE的internet选项->安全,将Internet/本地Internet/受信任的站定/受限制的站点中的启用保护模式所有去掉勾,或者所有勾上