欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python爬蟲selenium模塊詳解

 更新時間:2021年03月30日 09:42:04   作者:南岸青梔*  
這篇文章主要介紹了python爬蟲selenium模塊詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

selenium模塊

selenium基本概念

selenium優(yōu)勢

  • 便捷的獲取網(wǎng)站中動態(tài)加載的數(shù)據(jù)
  • 便捷實現(xiàn)模擬登陸

selenium使用流程:

1.環(huán)境安裝:pip install selenium

2.下載一個瀏覽器的驅動程序(谷歌瀏覽器)

3.實例化一個瀏覽器對象

基本使用

代碼

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()

基于瀏覽器自動化的操作

代碼

#編寫基于瀏覽器自動化的操作代碼

- 發(fā)起請求: get(url)

- 標簽定位: find系列的方法

- 標簽交互: send_ keys( 'xxx' )

- 執(zhí)行js程序: excute_script('jsCod')

- 前進,后退: back(),forward( )

- 關閉瀏覽器: 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/')

#標簽定位
search_input = bro.find_element_by_id('q')
sleep(2)
#執(zhí)行一組js代碼,使得滾輪向下滑動
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(2)
#標簽交互
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:

- 如果定位的標簽存在于iframe標簽之中,則必須使用switch_to.frame(id)

- 動作鏈(拖動) : from selenium. webdriver import ActionChains
	- 實例化一個動作鏈對象: action = ActionChains (bro)
	- click_and_hold(div) :長按且點擊操作
	- move_by_offset(x,y)
	- perform( )讓動作鏈立即執(zhí)行
	- action.release( )釋放動作鏈對象

代碼

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')

#動作鏈
action = ActionChains(bro)
action.click_and_hold(div)

for i in range(5):
 action.move_by_offset(17,0).perform()
 sleep(0.3)

#釋放動作鏈
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()

無頭瀏覽器和規(guī)避檢測

代碼

from selenium import webdriver
from time import sleep
#實現(xiàn)無可視化界面
from selenium.webdriver.chrome.options import Options
#實現(xiàn)規(guī)避檢測
from selenium.webdriver import ChromeOptions

#實現(xiàn)無可視化界面
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
#實現(xiàn)規(guī)避檢測
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()

到此這篇關于python爬蟲selenium模塊詳解的文章就介紹到這了,更多相關python爬蟲selenium模塊內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 簡單講解Python中的字符串與字符串的輸入輸出

    簡單講解Python中的字符串與字符串的輸入輸出

    這篇文章主要介紹了Python中的字符串與字符串的輸入輸出,Python3.x版本中默認以Unicode為編碼,省去了不少麻煩,需要的朋友可以參考下
    2016-03-03
  • python使用__slots__讓你的代碼更加節(jié)省內存

    python使用__slots__讓你的代碼更加節(jié)省內存

    如果要限制添加的屬性,例如,Student類只允許添加 name、gender和score 這3個屬性,就可以利用Python的一個特殊的slots來實現(xiàn)。這篇文章主要給大家介紹了關于python如何使用__slots__讓你的代碼更加節(jié)省內存的相關資料,需要的朋友可以參考下
    2018-09-09
  • python在openstreetmap地圖上繪制路線圖的實現(xiàn)

    python在openstreetmap地圖上繪制路線圖的實現(xiàn)

    這篇文章主要介紹了python在openstreetmap地圖上繪制路線圖的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • Python?webargs?模塊的簡單使用

    Python?webargs?模塊的簡單使用

    webargs是一個用于解析和驗證HTTP請求對象的Python庫,今天通過本文給大家介紹Python?webargs?模塊的安裝使用,感興趣的朋友一起看看吧
    2022-01-01
  • Pyqt5實現(xiàn)英文學習詞典

    Pyqt5實現(xiàn)英文學習詞典

    這篇文章主要為大家詳細介紹了Pyqt5實現(xiàn)英文學習詞典的相關方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • 為什么稱python為膠水語言

    為什么稱python為膠水語言

    在本篇文章里小編給大家分享的是關于python為何稱為膠水語言的相關知識點,需要的朋友們可以學習參考下。
    2020-06-06
  • 一篇文章帶你了解python標準庫--os模塊

    一篇文章帶你了解python標準庫--os模塊

    在本篇內容里小編給大家整理的是關于Python中os模塊及用法相關知識點,有興趣的朋友們可以學習下,希望能給你帶來幫助
    2021-08-08
  • 深入解析神經(jīng)網(wǎng)絡從原理到實現(xiàn)

    深入解析神經(jīng)網(wǎng)絡從原理到實現(xiàn)

    這篇文章主要介紹了深入解析神經(jīng)網(wǎng)絡從原理到實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • scrapy redis配置文件setting參數(shù)詳解

    scrapy redis配置文件setting參數(shù)詳解

    這篇文章主要介紹了scrapy redis配置文件setting參數(shù)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Eclipse + Python 的安裝與配置流程

    Eclipse + Python 的安裝與配置流程

    Eclipse的安裝是很容易的。Eclipse是基于java的一個應用程序,因此需要一個java的運行環(huán)境(JRE)才行。(我這里主要介紹windows下的安裝)
    2013-03-03

最新評論