<meta charset="utf-8">
1.獲取ajax數(shù)據(jù)的方式
- 直接分析ajax調(diào)用的接口。然后通過代碼請求這個接口摄闸。
- 使用Selenium+chromedriver模擬瀏覽器行為獲取數(shù)據(jù)。
方式 | 優(yōu)點(diǎn) | 缺點(diǎn) |
---|---|---|
分析接口 | 直接可以請求到數(shù)據(jù)雨女。不需要做一些解析工作匿辩。代碼量少,性能高源请。 | 分析接口比較復(fù)雜枪芒,特別是一些通過js混淆的接口,要有一定的js功底谁尸。容易被發(fā)現(xiàn)是爬蟲 |
selenium | 直接模擬瀏覽器的行為舅踪。瀏覽器能請求到的,使用selenium也能請求到良蛮。爬蟲更穩(wěn)定抽碌。 | 代碼量多。性能低决瞳。 |
2.Chromedriver
ChromeDriver下載地址:https://sites.google.com/a/chromium.org/chromedriver/downloads
3.Selenium+chromedriver安裝及使用
1.安裝Selenium
pip install selenium
2.引入chromedriver
from selenium import webdriver
# chromedriver的絕對路徑
driver_path = r'E:\chromedriver\chromedriver.exe'
# 初始化一個driver货徙,并且指定chromedriver的路徑
driver = webdriver.Chrome(executable_path=driver_path)
3.Selenium基本操作方法
- 基礎(chǔ)操作
# 請求網(wǎng)頁
driver.get("https://www.baidu.com/")
# 關(guān)閉網(wǎng)頁
driver.close()
# 關(guān)閉瀏覽器
driver.quit()
- 查找Html元素
# 根據(jù)ID來查找某個元素
driver.find_element_by_id('')
driver.find_element(BY.ID,"")
# 根據(jù)類名來查找某個元素
driver.find_element_by_class_name('')
driver.find_element(BY.CLASS_NAME,'')
# 根據(jù)name屬性來找查找某個元素
driver.find_element_by_name("")
driver.find_element(BY.NAME,"")
# 根據(jù)標(biāo)簽名來查找某個元素
driver.find_element_by_tag_name('')
driver.find_element(BY.TAG_NAME'')
# 根據(jù)xpath語法來查找某個元素
driver.find_element_by_xpath('//div')
driver.find_element(BY.XPATH,'//div')
# 根據(jù)css選擇器來查找某個元素
driver.find_element_by_css_selector('//div')
driver.find_element(BY.CSS_SELECTOR,'//div')
find_element是獲取第一個滿足條件的元素。find_elements是獲取所有滿足條件的元素皮胡。
- 操作表單元素
- 操作輸入框
# 找到輸入框元素
inputTag = driver.find_element_by_id('kw')
# 填充數(shù)據(jù)
inputTag.send_keys("python")
# 清除數(shù)據(jù)
inputTag.clear()
- 操作checkbox
# 選擇checkbox內(nèi)標(biāo)簽對應(yīng)的name值
rememberTag = driver.find_element_by_name("rememberMe")
# 執(zhí)行選中操作
rememberTag.click()
-
操作select
select元素不能直接點(diǎn)擊痴颊。因?yàn)辄c(diǎn)擊后還需要選中元素。這時候selenium就專門為select標(biāo)簽提供了一個類<mark style="box-sizing: border-box;">selenium.webdriver.support.ui.Select</mark>屡贺。將獲取到的元素當(dāng)成參數(shù)傳到這個類中蠢棱,創(chuàng)建這個對象锌杀。以后就可以使用這個對象進(jìn)行選擇了。
from selenium.webdriver.support.ui import Select
# 選中這個標(biāo)簽泻仙,然后使用Select創(chuàng)建對象
selectTag = Select(driver.find_element_by_name("jumpMenu"))
# 根據(jù)索引選擇
selectTag.select_by_index(1)
# 根據(jù)值選擇
selectTag.select_by_value("http://www.95yueba.com")
# 根據(jù)可視的文本選擇
selectTag.select_by_visible_text("95秀客戶端")
# 取消選中所有選項(xiàng)
selectTag.deselect_all()
- 操作按鈕
# 選中按鈕
inputTag = driver.find_element_by_id('su')
# 點(diǎn)擊
inputTag.click()
4.行為鏈
有時候在頁面中的操作可能要有很多步糕再,那么這時候可以使用鼠標(biāo)行為鏈類ActionChains來完成。比如現(xiàn)在要將鼠標(biāo)移動到某個元素上并執(zhí)行點(diǎn)擊事件玉转。
# 獲取輸入框
inputTag = driver.find_element_by_id('kw')
# 獲取提交按鈕
submitTag = driver.find_element_by_id('su')
#初始化行為鏈
actions = ActionChains(driver)
# 輸入框輸入內(nèi)容
actions.move_to_element(inputTag)
actions.send_keys_to_element(inputTag,'python')
# 點(diǎn)擊提交按鈕
actions.move_to_element(submitTag)
actions.click(submitTag)
# 執(zhí)行行為鏈
actions.perform()
還有更多的鼠標(biāo)相關(guān)的操作突想。
click_and_hold(element):點(diǎn)擊但不松開鼠標(biāo)。
context_click(element):右鍵點(diǎn)擊冤吨。
double_click(element):雙擊蒿柳。
5.Cookies操作
1.獲取所有cookies
for cookie in driver.get_cookies():
print(cookie)
2.根據(jù)cookie的key獲取value
value = driver.get_cookie(key)
3.刪除所有的cookie
driver.delete_all_cookies()
4.刪除某個cookie
driver.delete_cookie(key)
6.切換頁面
# 打開一個新的頁面
driver.execute_script("window.open('"+url+"')")
# 切換到這個新的頁面中
driver.switch_to_window(driver.window_handles[1])