日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

爬虫笔记:Python Selenium详解

發布時間:2024/9/30 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬虫笔记:Python Selenium详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.簡介
Selenium是一個用于測試網站的自動化測試工具,支持各種瀏覽器包括Chrome、Firefox、Safari等主流界面瀏覽器,同時也支持phantomJS無界面瀏覽器。
支持多種操作系統如Windows、Linux、IOS、Android等。
2.安裝
pip install Selenium
3.安裝瀏覽器驅動
當selenium升級到3.0之后,對不同的瀏覽器驅動進行了規范。如果想使用selenium驅動不同的瀏覽器,必須單獨下載并設置不同的瀏覽器驅動。

各瀏覽器下載地址:

Firefox瀏覽器驅動:https://github.com/mozilla/geckodriver/releases
Chrome瀏覽器驅動:鏈接1
鏈接2

IE瀏覽器驅動:http://selenium-release.storage.googleapis.com/index.html
Edge瀏覽器驅動:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Opera瀏覽器驅動:https://github.com/operasoftware/operachromiumdriver/releases
PhantomJS瀏覽器驅動:https://phantomjs.org/

1.基本使用

from selenium import webdriver#瀏覽器驅動對象 from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWaitpath='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 browser = webdriver.Chrome(path)#聲明一個瀏覽器對象 try:browser.get('https://www.baidu.com')#訪問網址input = browser.find_element_by_id('kw')input.send_keys('Python')#在鍵盤里輸入pythoninput.send_keys(Keys.ENTER)#輸入回車wait = WebDriverWait(browser, 10)#等待10秒wait.until(EC.presence_of_element_located((By.ID, 'content_left')))#等待ID為content_left加載出來print(browser.current_url)print(browser.get_cookies())print(browser.page_source)#源代碼 finally:browser.close()

2.聲明瀏覽器對象

from selenium import webdriverbrowser = webdriver.Chrome() browser = webdriver.Firefox() browser = webdriver.Edge() browser = webdriver.PhantomJS() browser = webdriver.Safari()

3.訪問頁面

from selenium import webdriver path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 browser = webdriver.Chrome(path) browser.get('https://www.taobao.com') print(browser.page_source)#打印源代碼 #browser.close()

瀏覽器會自動打開淘寶頁面,如果要關閉,可以在末尾添加browser.close()。
瀏覽器淘寶頁面會一閃而過

page_source

查找元素
4.查找單個元素

from selenium import webdriver path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 browser = webdriver.Chrome(path) browser.get('https://www.taobao.com') input_first = browser.find_element_by_id('q') input_second = browser.find_element_by_css_selector('#q') input_third = browser.find_element_by_xpath('//*[@id="q"]') print(input_first, input_second, input_third) browser.close()

定位元素方法

通用方法

from selenium import webdriver from selenium.webdriver.common.by import By path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 browser = webdriver.Chrome(path) browser.get('https://www.taobao.com') input_first = browser.find_element(By.ID, 'q')#通過id print(input_first) browser.close()

5.查找多個元素

from selenium import webdriver path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 browser = webdriver.Chrome(path) browser.get('https://www.taobao.com') lis = browser.find_elements_by_css_selector('.service-bd li') print(lis) browser.close() 選中的是左側導航欄標簽

通用寫法

from selenium import webdriver from selenium.webdriver.common.by import Bypath='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 browser = webdriver.Chrome(path) browser.get('https://www.taobao.com') lis = browser.find_elements(By.CSS_SELECTOR, '.service-bd li') print(lis) browser.close()

元素交互操作
6.對獲取的元素調用交互方法

from selenium import webdriver import timepath='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 browser = webdriver.Chrome(path) browser.get('https://www.taobao.com') input = browser.find_element_by_id('q')#捕捉輸入框 input.send_keys('iPhone')#在輸入框里輸入iphone time.sleep(1)#等待1秒 input.clear()#清除 input.send_keys('iPad')#再次輸入ipad button = browser.find_element_by_class_name('btn-search')#搜索按鈕 button.click()#點擊按鈕

結果如圖

分析輸入框id為q

點擊按鈕的class為btn-search

更多操作:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement

交互操作
7.交互動作,將動作附加到動作鏈中串行執行

from selenium import webdriver from selenium.webdriver import ActionChains path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 browser = webdriver.Chrome(path) url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' browser.get(url) browser.switch_to.frame('iframeResult') source = browser.find_element_by_css_selector('#draggable') target = browser.find_element_by_css_selector('#droppable') actions = ActionChains(browser) actions.drag_and_drop(source, target) actions.perform()

更多操作

執行JavaScript

from selenium import webdriver path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄browser = webdriver.Chrome(path) browser.get('https://www.zhihu.com/explore') browser.execute_script('window.scrollTo(0, document.body.scrollHeight)') browser.execute_script('alert("To Bottom")')

獲取元素信息
9.獲取屬性

from selenium import webdriver path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄from selenium.webdriver import ActionChainsbrowser = webdriver.Chrome(path) url = 'https://blog.csdn.net/KOBEYU652453/article/details/113743933' browser.get(url) logo = browser.find_element_by_id('article_content')#定位 print(logo) print(logo.get_attribute('class'))#獲取logo的class

結果

網頁中的class

10.獲取文本值

from selenium import webdriver path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄browser = webdriver.Chrome(path) url = 'https://blog.csdn.net/KOBEYU652453/article/details/113743933' browser.get(url)input = browser.find_element_by_class_name('article_content') print(input.text)

11.獲取ID、位置、標簽名、大小

from selenium import webdriver path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄browser = webdriver.Chrome(path) url = 'https://blog.csdn.net/KOBEYU652453/article/details/113743933' browser.get(url)input = browser.find_element_by_class_name('article_content') print(input.id) print(input.location) print(input.tag_name) print(input.size)

Frame

path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄import time from selenium import webdriver from selenium.common.exceptions import NoSuchElementExceptionbrowser = webdriver.Chrome(path) url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' browser.get(url) browser.switch_to.frame('iframeResult') source = browser.find_element_by_css_selector('#draggable') print(source) try:logo = browser.find_element_by_class_name('logo') except NoSuchElementException:print('NO LOGO') browser.switch_to.parent_frame() logo = browser.find_element_by_class_name('logo') print(logo) print(logo.text)

等待

當使用了隱式等待執行測試的時候,如果 WebDriver沒有在 DOM中找到元素,將繼續等待,超出設定時間后則拋出找不到元素的異常, 換句話說,當查找元素或元素并沒有立即出現的時候,隱式等待將等待一段時間再查找 DOM,默認的時間是0

from selenium import webdriver path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄browser = webdriver.Chrome(path)browser.implicitly_wait(10) browser.get('https://www.zhihu.com/explore') input = browser.find_element_by_class_name('zu-top-add-question') print(input)

顯示等待

path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as ECbrowser = webdriver.Chrome(path) browser.get('https://www.taobao.com/') wait = WebDriverWait(browser, 10) input = wait.until(EC.presence_of_element_located((By.ID, 'q')))#等待條件 button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search'))) print(input, button)

等待條件如下

* title_is 標題是某內容 * title_contains 標題包含某內容 * presence_of_element_located 元素加載出,傳入定位元組,如(By.ID, 'p') * visibility_of_element_located 元素可見,傳入定位元組 * visibility_of 可見,傳入元素對象 * presence_of_all_elements_located 所有元素加載出 * text_to_be_present_in_element 某個元素文本包含某文字 * text_to_be_present_in_element_value 某個元素值包含某文字 * frame_to_be_available_and_switch_to_it frame加載并切換 * invisibility_of_element_located 元素不可見 * element_to_be_clickable 元素可點擊 * staleness_of 判斷一個元素是否仍在DOM,可判斷頁面是否已經刷新 * element_to_be_selected 元素可選擇,傳元素對象 * element_located_to_be_selected 元素可選擇,傳入定位元組 * element_selection_state_to_be 傳入元素對象以及狀態,相等返回True,否則返回False * element_located_selection_state_to_be 傳入定位元組以及狀態,相等返回True,否則返回False * alert_is_present 是否出現Alert

等待詳細內容

前進后退

import time from selenium import webdriver path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 browser = webdriver.Chrome(path) browser.get('https://www.baidu.com/') browser.get('https://www.taobao.com/') browser.get('https://www.python.org/') browser.back() time.sleep(1) browser.forward() browser.close()

Cookies

from selenium import webdriver path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 browser = webdriver.Chrome(path) browser.get('https://www.zhihu.com/explore') print(browser.get_cookies())#打印cookies browser.add_cookie({'name': 'name', 'domain': 'www.zhihu.com', 'value': 'germey'})#添加cookies print(browser.get_cookies()) browser.delete_all_cookies()#刪除所有cookies print(browser.get_cookies())

選項卡管理

import time from selenium import webdriver path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 browser = webdriver.Chrome(path) browser.get('https://www.baidu.com') browser.execute_script('window.open()') print(browser.window_handles) browser.switch_to_window(browser.window_handles[1]) browser.get('https://www.taobao.com') time.sleep(1) browser.switch_to_window(browser.window_handles[0]) browser.get('https://python.org')

異常處理

path='D:\chromedriver_win32\chromedriver.exe'#驅動目錄 from selenium import webdriver from selenium.common.exceptions import TimeoutException, NoSuchElementExceptionbrowser = webdriver.Chrome(path) try:browser.get('https://www.baidu.com') except TimeoutException:print('Time Out') try:browser.find_element_by_id('hello') except NoSuchElementException:print('No Element') finally:browser.close()

詳細文檔
http://selenium-python.readthedocs.io/api.html#module-selenium.common.exceptions


總結

以上是生活随笔為你收集整理的爬虫笔记:Python Selenium详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。