Python中怎么利用selenium實(shí)現(xiàn)一個(gè)動(dòng)態(tài)爬蟲(chóng)

Python中怎么利用selenium實(shí)現(xiàn)一個(gè)動(dòng)態(tài)爬蟲(chóng),相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

創(chuàng)新互聯(lián)建站專(zhuān)業(yè)為企業(yè)提供尚志網(wǎng)站建設(shè)、尚志做網(wǎng)站、尚志網(wǎng)站設(shè)計(jì)、尚志網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、尚志企業(yè)網(wǎng)站模板建站服務(wù),十多年尚志做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

1. 安裝

selenium安裝比較簡(jiǎn)單,直接用pip就可以安裝,打開(kāi)cmd,輸入

pip install selenium

就好了

2.  安裝chromedriver

chromedriver是谷歌瀏覽器的驅(qū)動(dòng)程序,因?yàn)槲移綍r(shí)用chrome

這里需要注意的是,chromedriver的版本需要是你安裝的Chrome的版本對(duì)應(yīng)起來(lái),Chrome的版本可以在瀏覽器的右上角找到幫助-關(guān)于Google Chrome 查看瀏覽器的版本。具體的對(duì)應(yīng)規(guī)則如下:

chromedriver版本支持的Chrome版本
v2.40v66-68
v2.39v66-68
v2.38v65-67
v2.37v64-66
v2.36v63-65
v2.35v62-64
v2.34v61-63
v2.33v60-62
v2.32v59-61
v2.31v58-60
v2.30v58-60
v2.29v56-58
v2.28v55-57
v2.27v54-56
v2.26v53-55
v2.25v53-55
v2.24v52-54
v2.23v51-53
v2.22v49-52

安裝完之后,把驅(qū)動(dòng)的安裝目錄添加到系統(tǒng)Path中就好了,如果不添加,在運(yùn)行程序的時(shí)候就會(huì)報(bào)錯(cuò),提示你沒(méi)有添加到Path中。

3. 開(kāi)始爬蟲(chóng)

今天要爬取的網(wǎng)址是:https://www.upbit.com/service_center/notice,然后點(diǎn)擊翻頁(yè)按鈕,發(fā)現(xiàn)url并沒(méi)有變化,通過(guò)F12查看請(qǐng)求的地址變化,可以發(fā)現(xiàn),

https://www.upbit.com/service_center/notice?id=1

這里主要變化的就是后面的id,1,2,3,。。。依次類(lèi)推。

用selenium爬蟲(chóng)開(kāi)始前,需要定義好下面內(nèi)容

# 設(shè)置谷歌瀏覽器的選項(xiàng),
opt = webdriver.ChromeOptions()
# 將瀏覽器設(shè)置為無(wú)頭瀏覽器,即先爬蟲(chóng)時(shí),沒(méi)有顯示的瀏覽器
opt.set_headless()
# 瀏覽器設(shè)置為谷歌瀏覽器,并設(shè)置為上面設(shè)置的選項(xiàng)
browser = webdriver.Chrome(options=opt)

save = []
home = 'https://www.upbit.com/home'
# 創(chuàng)建好瀏覽器對(duì)象后,通過(guò)get()方法可以向?yàn)g覽器發(fā)送網(wǎng)址,
# 獲取網(wǎng)址信息
browser.get(home)
time.sleep(15)

然后是如何定位html的元素,在selenium中,定位元素的方法有

find_element_by_id(self, id_)
find_element_by_name(self, name)
find_element_by_class_name(self, name)
find_element_by_tag_name(self, name)
find_element_by_link_text(self, link_text)
find_element_by_partial_link_text(self, link_text)
find_element_by_xpath(self, xpath)
find_element_by_css_selector(self, css_selector)

其中的id,name等都可以通過(guò)瀏覽器獲得,定位元素的目的是為了獲取我們想要的信息,然后解析出來(lái)保存,通過(guò)調(diào)用tex方法可以獲得元素的文本信息。

下面把整個(gè)爬蟲(chóng)的代碼,貼出來(lái),供大家參考


from selenium import webdriver
import time
from tqdm import trange
from collections import OrderedDict
import pandas as pd


def stringpro(inputs):
   inputs = str(inputs)
   return inputs.strip().replace("\n", "").replace("\t", "").lstrip().rstrip()


opt = webdriver.ChromeOptions()
opt.set_headless()
browser = webdriver.Chrome(options=opt)

save = []
home = 'https://www.upbit.com/home'
browser.get(home)
time.sleep(15)
for page in trange(500):
   try:
       rows = OrderedDict()
       url = "https://www.upbit.com/" \
             "service_center/notice?id={}".format(page)
       browser.get(url)
       content = browser.find_element_by_class_name(
           name='txtB').text
       title_class = browser.find_element_by_class_name(
           name='titB')
       title = title_class.find_element_by_tag_name(
           'strong').text
       times_str = title_class.find_element_by_tag_name(
           'span').text
       times = times_str.split('|')[0].split(" ")[1:]
       num = times_str.split("|")[1].split(" ")[1]
       rows['title'] = title
       rows['times'] = " ".join(times)
       rows['num'] = num
       rows['content'] = stringpro(content)
       save.append(rows)
       print("{},{}".format(page, rows))
   except Exception as e:
       continue

df = pd.DataFrame(save)
df.to_csv("./datasets/www_upbit_com.csv", index=None)



看完上述內(nèi)容,你們掌握Python中怎么利用selenium實(shí)現(xiàn)一個(gè)動(dòng)態(tài)爬蟲(chóng)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

標(biāo)題名稱(chēng):Python中怎么利用selenium實(shí)現(xiàn)一個(gè)動(dòng)態(tài)爬蟲(chóng)
網(wǎng)站路徑:http://muchs.cn/article8/ghieip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站建設(shè)、網(wǎng)站策劃外貿(mào)建站、面包屑導(dǎo)航網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化