目錄
- 第一步:python中安裝selenium庫
- 第二步:下載谷歌瀏覽器驅(qū)動并合理放置
- 第三步:使用selenium爬取QQ音樂歌詞(簡單示例)
第一步:python中安裝selenium庫
和其他所有Python庫一樣,selenium庫需要安裝
pip install selenium # Windows電腦安裝selenium
pip3 install selenium # Mac電腦安裝selenium
第二步:下載谷歌瀏覽器驅(qū)動并合理放置
selenium的腳本可以控制所有常見瀏覽器,在使用之前需要安裝瀏覽器端的驅(qū)動
注意:驅(qū)動和瀏覽器要版本對應(yīng)
推薦使用Chrome瀏覽器:谷歌瀏覽器驅(qū)動
打開chrome瀏覽器,在網(wǎng)址欄中輸入chrome://version/打開關(guān)于版本頁面,根據(jù)版本信息下載相應(yīng)chrome驅(qū)動
![](/d/20211017/c581a5f1fadad3a76ba1f4626e9c09ea.gif)
![](/d/20211017/4e683534aa539baffa5314237c025eca.gif)
將驅(qū)動放在python的安裝目錄(我的python集成在Anaconda3)
![](/d/20211017/12a305c617740bee4596acef67a89534.gif)
第三步:使用selenium爬取QQ音樂歌詞(簡單示例)
import time
# 本地Chrome瀏覽器設(shè)置方法
from selenium import webdriver #從selenium庫中調(diào)用webdriver模塊
driver = webdriver.Chrome() # 設(shè)置引擎為Chrome,從本地打開一個Chrome瀏覽器
driver.get('https://y.qq.com/n/ryqq/songDetail/0002Pa3i2ZU2Ec') # 打開網(wǎng)頁
time.sleep(3) # 程序暫停3秒鐘,等待網(wǎng)頁加載完畢
div_p = driver.find_element_by_id('lrc_content') # 解析網(wǎng)頁并提取所需標(biāo)簽
print(div_p.text) # 打印文本
driver.close() # 關(guān)閉瀏覽器
![](/d/20211017/33652a10bbc1c4b794595f5ed69e56b9.gif)
![](/d/20211017/fd7144ea49d0939be80cbc98fc668aa5.gif)
![](/d/20211017/1c8bd882417ef53843cef8f495e2dbb7.gif)
到此這篇關(guān)于python爬蟲之selenium庫的安裝及使用教程的文章就介紹到這了,更多相關(guān)selenium庫的安裝使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python爬蟲基礎(chǔ)之selenium庫的用法總結(jié)
- Python中selenium庫的用法詳解
- Python Selenium庫的基本使用教程
- Python爬蟲之Selenium庫的使用方法
- python中selenium庫的基本使用詳解
- Python中Selenium庫使用教程詳解
- Python趣味實戰(zhàn)之手把手教你實現(xiàn)舉牌小人生成器