Python爬虫selenium、PhanmJs
生活随笔
收集整理的這篇文章主要介紹了
Python爬虫selenium、PhanmJs
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
selenium:可以模擬鼠標(biāo)進(jìn)行一些操作
實(shí)例1:實(shí)現(xiàn)自動(dòng)打開(kāi)google瀏覽器,進(jìn)行百度搜索,并關(guān)閉瀏覽器
from selenium import webdriver from time import sleep#指定安裝google瀏覽器驅(qū)動(dòng)程序 bro = webdriver.Chrome(r'E:\Spider爬蟲(chóng)視頻\day03\chromedriver_win32\chromedriver.exe')#打開(kāi)瀏覽器發(fā)起請(qǐng)求 bro.get('https://www.baidu.com')sleep(2)#定位到搜索框 my_text = bro.find_element_by_id('kw') #向搜索框中輸入一個(gè)關(guān)鍵字 my_text.send_keys('西紅柿首富')sleep(5)#定位到搜索按鈕 my_button = bro.find_element_by_id('su') my_button.click()sleep(6) #關(guān)閉瀏覽器 bro.quit()
?
PhanmJs:無(wú)界面瀏覽器
?
from selenium import webdriver from time import sleep#指定phantomjs驅(qū)動(dòng) bro = webdriver.PhantomJS(r'E:\Spider爬蟲(chóng)視頻\day03\phantomjs-2.1.1-windows\bin\phantomjs.exe')#打開(kāi)瀏覽器發(fā)起請(qǐng)求 bro.get('https://www.baidu.com') bro.save_screenshot('./首頁(yè).png')#用來(lái)截屏 sleep(2)#定位到搜索框 my_text = bro.find_element_by_id('kw') #向搜索框中輸入一個(gè)關(guān)鍵字 my_text.send_keys('人民幣') bro.save_screenshot('./2.png') sleep(2)#定位到搜索按鈕 my_button = bro.find_element_by_id('su') my_button.click()sleep(5) bro.save_screenshot('./3.png') #關(guān)閉瀏覽器 bro.quit()?
轉(zhuǎn)載于:https://www.cnblogs.com/yangzhizong/p/9714387.html
總結(jié)
以上是生活随笔為你收集整理的Python爬虫selenium、PhanmJs的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 面试题16: 数值的整数次方
- 下一篇: redis-初识