国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python+selenium+chromedriver實(shí)現(xiàn)爬蟲(chóng)示例代碼

瀏覽:7日期:2022-07-30 14:23:35

下載好所需程序

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)前的版本

python+selenium+chromedriver實(shí)現(xiàn)爬蟲(chó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ù)-選擇禁止

python+selenium+chromedriver實(shí)現(xiàn)爬蟲(chóng)示例代碼

安裝瀏覽器對(duì)應(yīng)的驅(qū)動(dòng)driver我這里用的是谷歌,選擇對(duì)應(yīng)的驅(qū)動(dòng)版本

驅(qū)動(dòng)的下載地址如下:

http://chromedriver.storage.googleapis.com/index.html

python+selenium+chromedriver實(shí)現(xiàn)爬蟲(chóng)示例代碼

python+selenium+chromedriver實(shí)現(xiàn)爬蟲(chóng)示例代碼

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 英山县| 通化县| 桑日县| 那曲县| 许昌县| 隆化县| 汝城县| 高碑店市| 冷水江市| 阿勒泰市| 会同县| 岳普湖县| 平舆县| 丹凤县| 红桥区| 林芝县| 通山县| 东阿县| 泸西县| 美姑县| 潼南县| 仁布县| 襄城县| 巴南区| 宜兰县| 文登市| 呼伦贝尔市| 老河口市| 错那县| 田阳县| 乌苏市| 忻州市| 武威市| 灵台县| 蓬安县| 远安县| 伽师县| 宕昌县| 凌海市| 惠州市| 乌拉特前旗|