Python中怎么利用selenium實現一個動態爬蟲,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
1. 安裝
selenium安裝比較簡單,直接用pip就可以安裝,打開cmd,輸入
pip install selenium
就好了
2. 安裝chromedriver
chromedriver是谷歌瀏覽器的驅動程序,因為我平時用chrome
這里需要注意的是,chromedriver的版本需要是你安裝的Chrome的版本對應起來,Chrome的版本可以在瀏覽器的右上角找到幫助-關于Google Chrome 查看瀏覽器的版本。具體的對應規則如下:
| chromedriver版本 | 支持的Chrome版本 |
|---|---|
| v2.40 | v66-68 |
| v2.39 | v66-68 |
| v2.38 | v65-67 |
| v2.37 | v64-66 |
| v2.36 | v63-65 |
| v2.35 | v62-64 |
| v2.34 | v61-63 |
| v2.33 | v60-62 |
| v2.32 | v59-61 |
| v2.31 | v58-60 |
| v2.30 | v58-60 |
| v2.29 | v56-58 |
| v2.28 | v55-57 |
| v2.27 | v54-56 |
| v2.26 | v53-55 |
| v2.25 | v53-55 |
| v2.24 | v52-54 |
| v2.23 | v51-53 |
| v2.22 | v49-52 |
安裝完之后,把驅動的安裝目錄添加到系統Path中就好了,如果不添加,在運行程序的時候就會報錯,提示你沒有添加到Path中。
3. 開始爬蟲
今天要爬取的網址是:https://www.upbit.com/service_center/notice,然后點擊翻頁按鈕,發現url并沒有變化,通過F12查看請求的地址變化,可以發現,
https://www.upbit.com/service_center/notice?id=1
這里主要變化的就是后面的id,1,2,3,。。。依次類推。
用selenium爬蟲開始前,需要定義好下面內容
# 設置谷歌瀏覽器的選項,
opt = webdriver.ChromeOptions()
# 將瀏覽器設置為無頭瀏覽器,即先爬蟲時,沒有顯示的瀏覽器
opt.set_headless()
# 瀏覽器設置為谷歌瀏覽器,并設置為上面設置的選項
browser = webdriver.Chrome(options=opt)
save = []
home = 'https://www.upbit.com/home'
# 創建好瀏覽器對象后,通過get()方法可以向瀏覽器發送網址,
# 獲取網址信息
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等都可以通過瀏覽器獲得,定位元素的目的是為了獲取我們想要的信息,然后解析出來保存,通過調用tex方法可以獲得元素的文本信息。
下面把整個爬蟲的代碼,貼出來,供大家參考
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)
看完上述內容,你們掌握Python中怎么利用selenium實現一個動態爬蟲的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。