python+selenium+chromedriver實(shí)現(xiàn)爬蟲示例代碼
下載好所需程序
1.Selenium簡介
Selenium是一個(gè)用于Web應(yīng)用程序測試的工具,直接運(yùn)行在瀏覽器中,就像真正的用戶在操作一樣。
2.Selenium安裝
方法一:在Windows命令行(cmd)輸入pip install selenium即可自動安裝,安裝完成后,輸入pip show selenium可查看當(dāng)前的版本
方法二:直接下載selenium包:
selenium下載網(wǎng)址
Pychome安裝selenium如果出現(xiàn)無法安裝,參考以下博客解決Pycharm無法使用已經(jīng)安裝Selenium的問題
3.禁止谷歌瀏覽器自動更新
搜索本地:管理工具-服務(wù)-Google自動更新服務(wù)-選擇禁止
安裝瀏覽器對應(yīng)的驅(qū)動driver我這里用的是谷歌,選擇對應(yīng)的驅(qū)動版本
驅(qū)動的下載地址如下:
http://chromedriver.storage.googleapis.com/index.html
win32、win64的都下載win32.zip的
將下載的chromedriver進(jìn)行解壓,并將文件復(fù)制或移動到,瀏覽器快捷方式所在目錄。
環(huán)境變量配置1.Python環(huán)境配置2.chromedriver環(huán)境配置3.pychrome的python環(huán)境指向自己電腦安裝好的python
注意:將下載好的chromewebdriver.exe驅(qū)動放在Python的安裝路徑下的Scripts里面,同時(shí)將Scripts路徑添加到PATH中,這樣每次運(yùn)行python的時(shí)候就會自動加載驅(qū)動
代碼實(shí)現(xiàn)
#已經(jīng)準(zhǔn)備環(huán)境:webdriver:Google已經(jīng)安裝好;環(huán)境變量配置好;pip install selenium;#selenium是一個(gè)包,包有很多對象,對象有屬性,方法。from selenium import webdriverbrowser=webdriver.Chrome()#打開瀏覽器url='https://news.qq.com/zt2020/page/feiyan.htm#/global?nojump=1'#獲取數(shù)據(jù)的地址#請求瀏覽器內(nèi)容:請求方式:get,post,tokenbrowser.get(url)#css選擇器,id選擇器:#開頭,class選擇器:.開頭,標(biāo)簽選擇器:p,span,div。coronavirus_countent=browser.find_element_by_class_name(’d’)#定位到class選擇器d這個(gè)內(nèi)容print(coronavirus_countent)#查看內(nèi)容,session,一種緩存機(jī)制,通過瀏覽器解析,然后緩存的內(nèi)容# <selenium.webdriver.remote.webelement.WebElement (session='a1aa22161543b44f599e97b35dbc1ac5', element='fe645993-43cb-46cf-83a7-2488dd3d838a')>print(coronavirus_countent.text)#查看當(dāng)前css.class中的d的內(nèi)容coronavirus_time=browser.find_element_by_class_name(’ml’)#定位到class選擇器d這個(gè)內(nèi)容print(coronavirus_time.text)coronavirus_data=browser.find_element_by_class_name(’nowConfirm’)#定位到class選擇器d這個(gè)內(nèi)容print('=======')print(coronavirus_data.text)print('=====找nowConfirm下面的字內(nèi)容')coronavirus_sub=coronavirus_data.find_element_by_class_name(’addnum’)print(coronavirus_sub.text)browser.quit()
到此這篇關(guān)于python+selenium+chromedriver實(shí)現(xiàn)爬蟲示例代碼的文章就介紹到這了,更多相關(guān)python selenium chromedriver 爬蟲內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. asp批量添加修改刪除操作示例代碼3. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)4. css代碼優(yōu)化的12個(gè)技巧5. 如何在jsp界面中插入圖片6. Vue element ui用戶展示頁面的實(shí)例7. Ajax返回值類型與用法實(shí)例分析8. 使用FormData進(jìn)行Ajax請求上傳文件的實(shí)例代碼9. .NET6打包部署到Windows Service的全過程10. HTML 絕對路徑與相對路徑概念詳細(xì)
