python爬蟲selenium模塊詳解
selenium模塊
selenium基本概念
selenium優(yōu)勢(shì)
- 便捷的獲取網(wǎng)站中動(dòng)態(tài)加載的數(shù)據(jù)
- 便捷實(shí)現(xiàn)模擬登陸
selenium使用流程:
1.環(huán)境安裝:pip install selenium
2.下載一個(gè)瀏覽器的驅(qū)動(dòng)程序(谷歌瀏覽器)
3.實(shí)例化一個(gè)瀏覽器對(duì)象
基本使用
代碼
from selenium import webdriver
from lxml import etree
from time import sleep
if __name__ == '__main__':
bro = webdriver.Chrome(r"E:\google\Chrome\Application\chromedriver.exe")
bro.get(url='http://scxk.nmpa.gov.cn:81/xk/')
page_text = bro.page_source
tree = etree.HTML(page_text)
li_list = tree.xpath('//*[@id="gzlist"]/li')
for li in li_list:
name = li.xpath('./dl/@title')[0]
print(name)
sleep(5)
bro.quit()
基于瀏覽器自動(dòng)化的操作
代碼
#編寫基于瀏覽器自動(dòng)化的操作代碼
- 發(fā)起請(qǐng)求: get(url)
- 標(biāo)簽定位: find系列的方法
- 標(biāo)簽交互: send_ keys( 'xxx' )
- 執(zhí)行js程序: excute_script('jsCod')
- 前進(jìn),后退: back(),forward( )
- 關(guān)閉瀏覽器: quit()
代碼
https://www.taobao.com/
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")
bro.get(url='https://www.taobao.com/')
#標(biāo)簽定位
search_input = bro.find_element_by_id('q')
sleep(2)
#執(zhí)行一組js代碼,使得滾輪向下滑動(dòng)
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(2)
#標(biāo)簽交互
search_input.send_keys('女裝')
button = bro.find_element_by_class_name('btn-search')
button.click()
bro.get('https://www.baidu.com')
sleep(2)
bro.back()
sleep(2)
bro.forward()
sleep(5)
bro.quit()
selenium處理iframe:
- 如果定位的標(biāo)簽存在于iframe標(biāo)簽之中,則必須使用switch_to.frame(id) - 動(dòng)作鏈(拖動(dòng)) : from selenium. webdriver import ActionChains - 實(shí)例化一個(gè)動(dòng)作鏈對(duì)象: action = ActionChains (bro) - click_and_hold(div) :長(zhǎng)按且點(diǎn)擊操作 - move_by_offset(x,y) - perform( )讓動(dòng)作鏈立即執(zhí)行 - action.release( )釋放動(dòng)作鏈對(duì)象
代碼
https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
bro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")
bro.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
bro.switch_to.frame('iframeResult')
div = bro.find_element_by_id('draggable')
#動(dòng)作鏈
action = ActionChains(bro)
action.click_and_hold(div)
for i in range(5):
action.move_by_offset(17,0).perform()
sleep(0.3)
#釋放動(dòng)作鏈
action.release()
bro.quit()
selenium模擬登陸QQ空間
代碼
https://qzone.qq.com/
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")
bro.get('https://qzone.qq.com/')
bro.switch_to.frame("login_frame")
switcher = bro.find_element_by_id('switcher_plogin')
switcher.click()
user_tag = bro.find_element_by_id('u')
password_tag = bro.find_element_by_id('p')
user_tag.send_keys('1234455')
password_tag.send_keys('qwer123')
sleep(1)
but = bro.find_element_by_id('login_button')
but.click()
無(wú)頭瀏覽器和規(guī)避檢測(cè)
代碼
from selenium import webdriver
from time import sleep
#實(shí)現(xiàn)無(wú)可視化界面
from selenium.webdriver.chrome.options import Options
#實(shí)現(xiàn)規(guī)避檢測(cè)
from selenium.webdriver import ChromeOptions
#實(shí)現(xiàn)無(wú)可視化界面
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
#實(shí)現(xiàn)規(guī)避檢測(cè)
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
bro = webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe",chrome_options=chrome_options,options=option)
bro.get('https://www.baidu.com')
print(bro.page_source)
sleep(2)
bro.quit()
到此這篇關(guān)于python爬蟲selenium模塊詳解的文章就介紹到這了,更多相關(guān)python爬蟲selenium模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python運(yùn)行環(huán)境在新舊電腦間遷移的三種方法
環(huán)境部署或遷移是一項(xiàng)簡(jiǎn)單而又考驗(yàn)應(yīng)對(duì)能力的一項(xiàng)工作,這篇文章主要給大家介紹了關(guān)于python運(yùn)行環(huán)境在新舊電腦間遷移的三種方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
python django model聯(lián)合主鍵的例子
今天小編就為大家分享一篇python django model聯(lián)合主鍵的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python中join函數(shù)簡(jiǎn)單代碼示例
這篇文章主要介紹了Python中join函數(shù)簡(jiǎn)單代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
python提取具有某種特定字符串的行數(shù)據(jù)方法
今天小編就為大家分享一篇python提取具有某種特定字符串的行數(shù)據(jù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Python3 合并二叉樹(shù)的實(shí)現(xiàn)
這篇文章主要介紹了Python3 合并二叉樹(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
使用Python實(shí)現(xiàn)控制攝像頭的方法詳解
當(dāng)今,隨著計(jì)算機(jī)技術(shù)的發(fā)展,攝像頭已經(jīng)成為了人們生活中不可或缺的一部分。而Python作為一種流行的編程語(yǔ)言,也可以輕松地控制和操作攝像頭。本文將介紹如何使用Python中的常用庫(kù)(例如OpenCV和Tkinter)來(lái)控制和操作攝像頭,需要的可以參考一下2023-03-03
python學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)實(shí)例代碼
數(shù)據(jù)結(jié)構(gòu)就是用來(lái)將數(shù)據(jù)組織在一起的結(jié)構(gòu)。換句話說(shuō),數(shù)據(jù)結(jié)構(gòu)是用來(lái)存儲(chǔ)一系列關(guān)聯(lián)數(shù)據(jù)的東西。在Python中有四種內(nèi)建的數(shù)據(jù)結(jié)構(gòu),分別是List、Tuple、Dictionary以及Set。本文將通過(guò)實(shí)例來(lái)介紹這些數(shù)據(jù)結(jié)構(gòu)的用法。2015-05-05
Pytorch訓(xùn)練過(guò)程出現(xiàn)nan的解決方式
今天小編就為大家分享一篇Pytorch訓(xùn)練過(guò)程出現(xiàn)nan的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01

