天天看點

python網絡爬蟲之selenium爬取執行個體

python網絡爬蟲之selenium

今天終于進入到了selenium的學習,使用click()方法對百度首頁進行了測試,目的是爬取全部百度熱搜。

python網絡爬蟲之selenium爬取執行個體

除了擷取第一頁展示的6個标題外,還要通過 click()方法模拟點選 “換一換” 按鈕擷取剩下3頁的(一共4頁)

注釋滿滿的代碼:

from selenium import webdriver
# 目标網址
url = 'https://www.baidu.com/'
# 驅動火狐浏覽器
driver = webdriver.Firefox(executable_path='D:\develop\geckodriver.exe')
# 加載網址
driver.get(url)
# 隐式等待10s
driver.implicitly_wait(10)
number = []


def repeat():
    k = 'count'
    # 因為百度首頁将熱搜序号1 2 3 分别以1 3 5的序号排在list裡,是以需要跨步為2進行循環
    for i in range(1, 7, 2):
        element_odd = driver.find_element_by_css_selector('li.hotsearch-item:nth-child(' + str(i) + ')')
        print(element_odd.text)
    # 與上面同理,這樣在控制台輸出的才是 1 2 3 4 5 6...這樣的順序 否則就是135246這樣
    for j in range(2, 8, 2):
        element_even = driver.find_element_by_css_selector('li.hotsearch-item:nth-child(' + str(j) + ')')
        print(element_even.text)
        if j == 6:
        	# 每次循環完一個頁面就往清單中加個字元串count
            number.append(k)
            if len(number) <= 5:
                number.append(k)
                # 通過css選擇器擷取 “換一換” 按鈕,再通過click()模拟點選
                driver.find_element_by_css_selector('#hotsearch-refresh-btn > i:nth-child(1)').click()
                # 疊代,方法的出口是 當建立的清單的長度小于等于5
                repeat()

# 運作repeat()方法
repeat()

           

運作結果:

python網絡爬蟲之selenium爬取執行個體

如何擷取css選擇器的位置