詳解Selenium+PhantomJS+python簡(jiǎn)單實(shí)現(xiàn)爬蟲(chóng)的功能
Selenium
一、簡(jiǎn)介
selenium是一個(gè)用于Web應(yīng)用自動(dòng)化程序測(cè)試的工具,測(cè)試直接運(yùn)行在瀏覽器中,就像真正的用戶(hù)在操作一樣
selenium2支持通過(guò)驅(qū)動(dòng)真實(shí)瀏覽器(FirfoxDriver,IternetExplorerDriver,OperaDriver,ChromeDriver)
selenium2支持通過(guò)驅(qū)動(dòng)無(wú)界面瀏覽器(HtmlUnit,PhantomJs)
二、安裝
Windows
第一種方法是:下載源碼安裝,下載地址(https://pypi.python.org/pypi/selenium)解壓并把整個(gè)目錄放到C:\Python27\Lib\site-packages下面
第二種方法是:可以直接在C:\Python27\Scripts 下輸入命令安裝 pip install -U selenium
sudo pip install selenium
PhantomJS
一、簡(jiǎn)介
PhantomJS 是一個(gè)基于 WebKit(WebKit是一個(gè)開(kāi)源的瀏覽器引擎,Chrome,Safari就是用的這個(gè)瀏覽器引擎) 的服務(wù)器端 JavaScript API,主要應(yīng)用場(chǎng)景是:無(wú)需瀏覽器的 Web 測(cè)試,頁(yè)面訪(fǎng)問(wèn)自動(dòng)化,屏幕捕獲,網(wǎng)絡(luò)監(jiān)控
二、安裝
Windows
下載源碼安裝,下載地址(http://phantomjs.org/download.html)解壓并把解壓縮的路徑添加到環(huán)境變量中即可,我自己的放到了C:\Python27\Scripts 下面
Linux
sudo apt-get install PhantomJS
Selenium + PhantomJS + python 簡(jiǎn)單實(shí)現(xiàn)爬蟲(chóng)的功能
python可以使用selenium執(zhí)行javascript,selenium可以讓瀏覽器自動(dòng)加載頁(yè)面,獲取需要的數(shù)據(jù)。selenium自己不帶瀏覽器,可以使用第三方瀏覽器如Firefox,Chrome等,也可以使用headless瀏覽器如PhantomJS在后臺(tái)執(zhí)行。
在工作用遇到一個(gè)問(wèn)題,當(dāng)加載一個(gè)手機(jī)端的URL時(shí)候,會(huì)加載不上,需要我們?cè)谡?qǐng)求頭中設(shè)置一個(gè)User-Agent,設(shè)置完以后就可以打開(kāi)了(Windows下執(zhí)行,linux下執(zhí)行的話(huà)就不用加executable_path='C:\Python27\Scripts\phantomjs.exe')
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
dcap = dict(DesiredCapabilities.PHANTOMJS) #設(shè)置userAgent
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0 ")
obj = webdriver.PhantomJS(executable_path='C:\Python27\Scripts\phantomjs.exe',desired_capabilities=dcap) #加載網(wǎng)址
obj.get('http://wap.95533pc.com')#打開(kāi)網(wǎng)址
obj.save_screenshot("1.png") #截圖保存
obj.quit() # 關(guān)閉瀏覽器。當(dāng)出現(xiàn)異常時(shí)記得在任務(wù)瀏覽器中關(guān)閉PhantomJS,因?yàn)闀?huì)有多個(gè)PhantomJS在運(yùn)行狀態(tài),影響電腦性能
一、超時(shí)設(shè)置
webdriver類(lèi)中有三個(gè)和時(shí)間相關(guān)的方法:
1.pageLoadTimeout 設(shè)置頁(yè)面完全加載的超時(shí)時(shí)間,完全加載即完全渲染完成,同步和異步腳本都執(zhí)行完
2.setScriptTimeout 設(shè)置異步腳本的超時(shí)時(shí)間
3.implicitlyWait 識(shí)別對(duì)象的智能等待時(shí)間
下面我們以獲取?;ňW(wǎng)title為例來(lái)驗(yàn)證效果,因?yàn)樾;ňW(wǎng)中圖片比較多,所以加載的時(shí)間比較長(zhǎng),更能時(shí)間我們的效果(另一原因我就不說(shuō)了,這樣才能讓我們學(xué)起來(lái)帶勁,哈哈?。。。?/p>
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
try:
obj.get('http://www.xiaohuar.com')
print obj.title
except Exception as e:
print e
二、元素的定位
對(duì)象的定位是通過(guò)屬性定位來(lái)實(shí)現(xiàn)的,這種屬性就像人的身份證信息一樣,或是其他的一些信息來(lái)找到這個(gè)對(duì)象,那我們下面就介紹下Webdriver提供的幾個(gè)常用的定位方法
<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
上面這個(gè)是百度的輸入框,我們可以發(fā)現(xiàn)我們可以用id來(lái)定位這個(gè)標(biāo)簽,然后就可以進(jìn)行后面的操作了
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
try:
obj.get('http://www.baidu.com')
obj.find_element_by_id('kw') #通過(guò)ID定位
obj.find_element_by_class_name('s_ipt') #通過(guò)class屬性定位
obj.find_element_by_name('wd') #通過(guò)標(biāo)簽name屬性定位
obj.find_element_by_tag_name('input') #通過(guò)標(biāo)簽屬性定位
obj.find_element_by_css_selector('#kw') #通過(guò)css方式定位
obj.find_element_by_xpath("http://input[@id='kw']") #通過(guò)xpath方式定位
obj.find_element_by_link_text("貼吧") #通過(guò)xpath方式定位
print obj.find_element_by_id('kw').tag_name #獲取標(biāo)簽的類(lèi)型
except Exception as e:
print e
三、瀏覽器的操作
1、調(diào)用啟動(dòng)的瀏覽器不是全屏的,有時(shí)候會(huì)影響我們的某些操作,所以我們可以設(shè)置全屏
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
obj.maximize_window() #設(shè)置全屏
try:
obj.get('http://www.baidu.com')
obj.save_screenshot('11.png') # 截取全屏,并保存
except Exception as e:
print e
2、設(shè)置瀏覽器寬、高
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
obj.set_window_size('480','800') #設(shè)置瀏覽器寬480,高800
try:
obj.get('http://www.baidu.com')
obj.save_screenshot('12.png') # 截取全屏,并保存
except Exception as e:
print e
3、操作瀏覽器前進(jìn)、后退
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
try:
obj.get('http://www.baidu.com') #訪(fǎng)問(wèn)百度首頁(yè)
obj.save_screenshot('1.png')
obj.get('http://www.sina.com.cn') #訪(fǎng)問(wèn)新浪首頁(yè)
obj.save_screenshot('2.png')
obj.back() #回退到百度首頁(yè)
obj.save_screenshot('3.png')
obj.forward() #前進(jìn)到新浪首頁(yè)
obj.save_screenshot('4.png')
except Exception as e:
print e
四、操作測(cè)試對(duì)象
定位到元素以后,我們就應(yīng)該對(duì)相應(yīng)的對(duì)象進(jìn)行某些操作,以達(dá)到我們某些特定的目的,那我們下面就介紹下Webdriver提供的幾個(gè)常用的操作方法
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
try:
obj.get('http://www.baidu.com')
print obj.find_element_by_id("cp").text # 獲取元素的文本信息
obj.find_element_by_id('kw').clear() #用于清除輸入框的內(nèi)容
obj.find_element_by_id('kw').send_keys('Hello') #在輸入框內(nèi)輸入Hello
obj.find_element_by_id('su').click() #用于點(diǎn)擊按鈕
obj.find_element_by_id('su').submit() #用于提交表單內(nèi)容
except Exception as e:
print e
五、鍵盤(pán)事件
1、鍵盤(pán)按鍵用法
from selenium.webdriver.common.keys import Keys
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
try:
obj.get('http://www.baidu.com')
obj.find_element_by_id('kw').send_keys(Keys.TAB) #用于清除輸入框的內(nèi)容,相當(dāng)于clear()
obj.find_element_by_id('kw').send_keys('Hello') #在輸入框內(nèi)輸入Hello
obj.find_element_by_id('su').send_keys(Keys.ENTER) #通過(guò)定位按鈕,通過(guò)enter(回車(chē))代替click()
except Exception as e:
print e
2、鍵盤(pán)組合鍵使用
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
try:
obj.get('http://www.baidu.com')
obj.find_element_by_id('kw').send_keys(Keys.TAB) #用于清除輸入框的內(nèi)容,相當(dāng)于clear()
obj.find_element_by_id('kw').send_keys('Hello') #在輸入框內(nèi)輸入Hello
obj.find_element_by_id('kw').send_keys(Keys.CONTROL,'a') #ctrl + a 全選輸入框內(nèi)容
obj.find_element_by_id('kw').send_keys(Keys.CONTROL,'x') #ctrl + x 剪切輸入框內(nèi)容
except Exception as e:
print e
六、中文亂碼問(wèn)題
selenium2 在python的send_keys()中輸入中文會(huì)報(bào)錯(cuò),其實(shí)在中文前面加一個(gè)u變成unicode就能搞定了
七、鼠標(biāo)事件
1、鼠標(biāo)右擊
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
try:
obj.get("http://pan.baidu.com")
obj.find_element_by_id('TANGRAM__PSP_4__userName').send_keys('13201392325') #定位并輸入用戶(hù)名
obj.find_element_by_id('TANGRAM__PSP_4__password').send_keys('18399565576lu') #定位并輸入密碼
obj.find_element_by_id('TANGRAM__PSP_4__submit').submit() #提交表單內(nèi)容
f = obj.find_element_by_xpath('/html/body/div/div[2]/div[2]/....') #定位到要點(diǎn)擊的標(biāo)簽
ActionChains(obj).context_click(f).perform() #對(duì)定位到的元素進(jìn)行右鍵點(diǎn)擊操作
except Exception as e:
print e
2、鼠標(biāo)雙擊
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
try:
obj.get("http://pan.baidu.com")
obj.find_element_by_id('TANGRAM__PSP_4__userName').send_keys('13201392325') #定位并輸入用戶(hù)名
obj.find_element_by_id('TANGRAM__PSP_4__password').send_keys('18399565576lu') #定位并輸入密碼
obj.find_element_by_id('TANGRAM__PSP_4__submit').submit() #提交表單內(nèi)容
f = obj.find_element_by_xpath('/html/body/div/div[2]/div[2]/....') #定位到要點(diǎn)擊的標(biāo)簽
ActionChains(obj).double_click(f).perform() #對(duì)定位到的元素進(jìn)行雙擊操作
except Exception as e:
print e
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
樹(shù)莓派(python)與arduino串口通信的詳細(xì)步驟
這篇文章主要介紹了樹(shù)莓派(python)與arduino串口通信的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
Python實(shí)現(xiàn)購(gòu)物車(chē)功能的方法分析
這篇文章主要介紹了Python實(shí)現(xiàn)購(gòu)物車(chē)功能的方法,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)購(gòu)物車(chē)功能的具體步驟、相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-11-11
Python接口自動(dòng)化淺析數(shù)據(jù)驅(qū)動(dòng)原理
這篇文章主要介紹了Python接口自動(dòng)化淺析數(shù)據(jù)驅(qū)動(dòng)原理,文中會(huì)詳細(xì)描述怎樣使用openpyxl模塊操作excel及結(jié)合ddt來(lái)實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng),有需要的朋友可以參考下2021-08-08
python爬蟲(chóng)使用requests發(fā)送post請(qǐng)求示例詳解
這篇文章主要介紹了python爬蟲(chóng)使用requests發(fā)送post請(qǐng)求示例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Python3.5多進(jìn)程原理與用法實(shí)例分析
這篇文章主要介紹了Python3.5多進(jìn)程原理與用法,結(jié)合實(shí)例形式分析了多進(jìn)程的原理、單進(jìn)程、多進(jìn)程、進(jìn)程類(lèi)及進(jìn)程隊(duì)列等相關(guān)定義與使用技巧,需要的朋友可以參考下2019-04-04
Python使用sqlite3第三方庫(kù)讀寫(xiě)SQLite數(shù)據(jù)庫(kù)的方法步驟
數(shù)據(jù)庫(kù)非常重要,程序的數(shù)據(jù)增刪改查需要數(shù)據(jù)庫(kù)支持,python處理數(shù)據(jù)庫(kù)非常簡(jiǎn)單,而且不同類(lèi)型的數(shù)據(jù)庫(kù)處理邏輯方式大同小異,下面這篇文章主要給大家介紹了關(guān)于Python使用sqlite3第三方庫(kù)讀寫(xiě)SQLite數(shù)據(jù)庫(kù)的方法步驟,需要的朋友可以參考下2022-07-07
python導(dǎo)入時(shí)小括號(hào)大作用
這篇文章主要介紹了python導(dǎo)入時(shí)小括號(hào)的大作用,非常的簡(jiǎn)單實(shí)用,希望這個(gè)小技巧能夠幫到大家2017-01-01

