1. 程式人生 > >Python爬蟲框架 Mac安裝selenium

Python爬蟲框架 Mac安裝selenium

文章目錄

一、終端安裝

[localhost:~ guohan$ pip install selenium
Collecting selenium
  Downloading https://files.pythonhosted.org/packages/b8/53/9cafbb616d20c7624ff31bcabd82e5cc9823206267664e68aa8acdde4629/selenium-3.14.0-py2.py3-none-any.whl (898kB)
    100% |████████████████████████████████| 901kB 290kB/s 
Requirement already satisfied: urllib3 in /usr/local/lib/python3.6/site-packages (from selenium) (1.23)
Installing collected packages: selenium
Successfully installed selenium-3.14.0
localhost:~ guohan$ python3 
Python 3.6.2 (default, Jul 17 2017, 16:44:47) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> print(1)

顯示安裝成功

二、pycharm安裝匯入selenium包

開啟一個python檔案輸入from selenium import webdriver發現報錯,證明pycharm沒有selenium包,需要手動匯入

1、開啟Pycharm --> Prereference --> Project:… --> Project Interpreter

檢視到列表中沒有selenium包

2、點選列表下左下側的+號,彈出頁面搜尋selenium,然後點選右下側install匯入包

顯示匯入成功,不再報錯。

3、也可以通過file --> DefaultSettings --> Project Interpeter來選擇python的安裝庫來匯入,此時,只要python包匯入了也就是終端匯入了,此處就可以直接用,相當於這是引用已匯入的庫

三、測試使用

1、新建python檔案,注意名字不要是selenium.py,否則與庫中的檔案重名而報錯

2、執行以下程式碼

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.baidu.com")

# Record the search window
search_windows = driver.current_window_handle

# Deal with Baidu search
elem = driver.find_element_by_id("kw")
elem.clear()

elem.send_keys("pycon")
elem.send_keys("python selenium programmer")
search = driver.find_element_by_id("su")
search.click()

# Open a registration page
login = driver.find_element_by_name('tj_login')
login.click()
driver.find_element_by_link_text(u'立即註冊').click()

# Get all window handles
all_handles = driver.window_handles

# If handle is not search, input some thing in registration page
for handle in all_handles:
    if handle != search_windows:
       driver.switch_to.window(handle)
       print('now register window!')
       driver.find_element_by_name("userName").send_keys('username')
       driver.find_element_by_name('phone').send_keys('password')
       time.sleep(2)

# If handle is search, switch to the search windows and do some search again
for handle in all_handles:
    if handle == search_windows:
       driver.switch_to.window(search_windows)
       print('now sreach window!')
       driver.find_element_by_id('TANGRAM__PSP_2__closeBtn').click()
       driver.find_element_by_id("kw").send_keys("selenium")
       driver.find_element_by_id("su").click()
       time.sleep(2)

driver.close()
  1. 報錯:
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

解決方法:下載-解壓-配置環境變數-測試
下載:選擇適合自己系統的版本

介紹:https://remarkablemark.org/blog/2016/11/06/selenium-geckodriver/
下載:https://github.com/mozilla/geckodriver/releases

配置環境變數:將其配置在path的最前面,否則pycharm還是找不到,由於解壓後的geckodriver就是可執行檔案,因此配置環境變數時直接到上級目錄即可。

cd ~
vim .bash_profile

測試如上程式碼,自動開啟火狐瀏覽器,成功安裝