python+selenium+chromedriver實(shí)現(xiàn)爬蟲(chóng)示例代碼
下載好所需程序
1.Selenium簡(jiǎn)介
Selenium是一個(gè)用于Web應(yīng)用程序測(cè)試的工具,直接運(yùn)行在瀏覽器中,就像真正的用戶在操作一樣。
2.Selenium安裝
方法一:在Windows命令行(cmd)輸入pip install selenium即可自動(dòng)安裝,安裝完成后,輸入pip show selenium可查看當(dāng)前的版本
方法二:直接下載selenium包:
selenium下載網(wǎng)址
Pychome安裝selenium如果出現(xiàn)無(wú)法安裝,參考以下博客解決Pycharm無(wú)法使用已經(jīng)安裝Selenium的問(wèn)題
3.禁止谷歌瀏覽器自動(dòng)更新
搜索本地:管理工具-服務(wù)-Google自動(dòng)更新服務(wù)-選擇禁止
安裝瀏覽器對(duì)應(yīng)的驅(qū)動(dòng)driver我這里用的是谷歌,選擇對(duì)應(yīng)的驅(qū)動(dòng)版本
驅(qū)動(dòng)的下載地址如下:
http://chromedriver.storage.googleapis.com/index.html
win32、win64的都下載win32.zip的
將下載的chromedriver進(jìn)行解壓,并將文件復(fù)制或移動(dòng)到,瀏覽器快捷方式所在目錄。
環(huán)境變量配置1.Python環(huán)境配置2.chromedriver環(huán)境配置3.pychrome的python環(huán)境指向自己電腦安裝好的python
注意:將下載好的chromewebdriver.exe驅(qū)動(dòng)放在Python的安裝路徑下的Scripts里面,同時(shí)將Scripts路徑添加到PATH中,這樣每次運(yùn)行python的時(shí)候就會(huì)自動(dòng)加載驅(qū)動(dòng)
代碼實(shí)現(xiàn)
#已經(jīng)準(zhǔn)備環(huán)境:webdriver:Google已經(jīng)安裝好;環(huán)境變量配置好;pip install selenium;#selenium是一個(gè)包,包有很多對(duì)象,對(duì)象有屬性,方法。from selenium import webdriverbrowser=webdriver.Chrome()#打開(kāi)瀏覽器url='https://news.qq.com/zt2020/page/feiyan.htm#/global?nojump=1'#獲取數(shù)據(jù)的地址#請(qǐng)求瀏覽器內(nèi)容:請(qǐng)求方式:get,post,tokenbrowser.get(url)#css選擇器,id選擇器:#開(kāi)頭,class選擇器:.開(kāi)頭,標(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ī)制,通過(guò)瀏覽器解析,然后緩存的內(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)爬蟲(chóng)示例代碼的文章就介紹到這了,更多相關(guān)python selenium chromedriver 爬蟲(chóng)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……2. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法3. 得到XML文檔大小的方法4. ASP使用MySQL數(shù)據(jù)庫(kù)的方法5. WMLScript的語(yǔ)法基礎(chǔ)6. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享7. xml中的空格之完全解說(shuō)8. html小技巧之td,div標(biāo)簽里內(nèi)容不換行9. XML入門的常見(jiàn)問(wèn)題(四)10. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法
