wordpress 筆記本網(wǎng)站seo策劃方案實(shí)例
此篇文章主要介紹如何使用 Selenium 模塊實(shí)現(xiàn) 無界面模式 & 執(zhí)行JS腳本(把滾動條拉到底部),并以具體的示例進(jìn)行展示。
1、Selenium 設(shè)置無界面模式
創(chuàng)建瀏覽器對象之前,創(chuàng)建 options 功能對象 :options = webdriver.ChromeOptions()
添加無界面功能參數(shù):options.add_argument("--headless")
構(gòu)造瀏覽器對象,打開瀏覽器,并設(shè)置 options 參數(shù):
browser = webdriver.Chrome(options=options)
from selenium import webdriver
options = webdriver.ChromeOptions() ?# 創(chuàng)建瀏覽器對象之前,創(chuàng)建options功能對象
options.add_argument("--headless") ?# 添加無界面功能參數(shù)
browser = webdriver.Chrome(options=options) ?# 構(gòu)造瀏覽器對象,打開瀏覽器
2、Selenium 執(zhí)行JS腳本
創(chuàng)建瀏覽器對象:browser = webdriver.Chrome()
執(zhí)行JS腳本:browser.execute_script()
最常用腳本 - 把滾動條拉到底部:browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
from selenium import webdriver
browser = webdriver.Chrome() # 創(chuàng)建瀏覽器對象
browser.execute_script(
? ? 'window.scrollTo(0,document.body.scrollHeight)'
) # 把滾動條拉到最底部
3、Selenium 設(shè)置無界面模式 & 執(zhí)行JS腳本 案例
3.1 需求分析
基于 Selenium + Chrome 抓取 `http://www.jd.com/` 下 “python書籍” 的信息
3.2 爬蟲思路
打開瀏覽器輸入主頁地址:https://www.jd.com/
使用 Selenium 的 Xpath 找到 信息輸入框 和 點(diǎn)擊搜索 節(jié)點(diǎn):'//*[@id="key"]' & '//*[@id="search"]/div/div[2]/button'
輸入 “python書籍” 并點(diǎn)擊 點(diǎn)擊搜索按鈕;
使用 Selenium 的 Xpath 找到 書籍信息 節(jié)點(diǎn)對象列表: '//*[@id="J_goodsList"]/ul/li';?
依次遍歷每個(gè)元素,并依次提取每本書籍信息;
爬取完一頁信息后,需要判斷是否是最后一頁
可以看到:
最后一頁的節(jié)點(diǎn)信息為:pn-next disabled
非最后一頁的節(jié)點(diǎn)信息為:pn-next
如果不是最后一頁,點(diǎn)擊下一頁繼續(xù)進(jìn)行爬取:'//*[@id="J_bottomPage"]/span[1]/a[9]'
3.3 程序?qū)崿F(xiàn)
初始化函數(shù)
? ? def __init__(self):
? ? ? ? # 設(shè)置為無界面
? ? ? ? self.options = webdriver.ChromeOptions() ?# 創(chuàng)建瀏覽器對象之前,創(chuàng)建options功能對象
? ? ? ? self.options.add_argument('--headless') ?# 添加無界面功能參數(shù)
? ? ? ? self.driver = webdriver.Chrome(options=self.options) ?# 構(gòu)造瀏覽器對象,打開瀏覽器
? ? ? ? self.driver.get(url="http://www.jd.com/") ?# 進(jìn)入主頁
? ? ? ? # 搜索框發(fā)送:python書籍,點(diǎn)擊搜索按鈕
? ? ? ? self.inputJD = self.driver.find_element(By.XPATH, '//*[@id="key"]') ?# 搜索框xpath://*[@id="key"]
? ? ? ? self.inputJD.send_keys("python書籍")
? ? ? ? self.driver.find_element(By.XPATH,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'//*[@id="search"]/div/div[2]/button').click() ?# 搜索按鈕xpath://*[@id="search"]/div/div[2]/button 并點(diǎn)擊
? ? ? ? time.sleep(1) ?# 要給頁面元素加載預(yù)留時(shí)間
提取數(shù)據(jù)函數(shù)
? ? def parse_html(self):
? ? ? ? """
? ? ? ? function: ?具體提取數(shù)據(jù)方法
? ? ? ? ? ? ? in: ?None
? ? ? ? ? ? ?out: ?None
? ? ? ? ? return: ?None
? ? ? ? ? others: ?Data Extraction Func
? ? ? ? """
? ? ? ? self.driver.execute_script(
? ? ? ? ? ? 'window.scrollTo(0,document.body.scrollHeight)'
? ? ? ? ) ?# 先把滾動條拉到最底部,等待所有商品加載完成再進(jìn)行數(shù)據(jù)爬取
? ? ? ? time.sleep(3) ?# 給頁面元素加載預(yù)留時(shí)間
? ? ? ? # 具體提取數(shù)據(jù)
? ? ? ? li_list = self.driver.find_elements(By.XPATH,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '//*[@id="J_goodsList"]/ul/li') ?# 基準(zhǔn)xpath://*[@id="J_goodsList"]/ul/li 每一個(gè)商品對應(yīng)一個(gè)li節(jié)點(diǎn)
? ? ? ? item = {} ?# 定義一個(gè)空字典
? ? ? ? for li in li_list:
? ? ? ? ? ? item["名稱"] = li.find_element(By.XPATH, './/div[@class="p-name"]/a/em').text.strip()
? ? ? ? ? ? item["價(jià)格"] = li.find_element(By.XPATH, './/div[@class="p-price"]/strong').text.strip()
? ? ? ? ? ? item["評價(jià)"] = li.find_element(By.XPATH, './/div[@class="p-commit"]/strong').text.strip()
? ? ? ? ? ? item["商家"] = li.find_element(By.XPATH, './/div[@class="p-shopnum"]').text.strip()
? ? ? ? ? ? print(item) ?# 打印
程序入口函數(shù)
? ? def run(self):
? ? ? ? """
? ? ? ? function: ?程序入口函數(shù)
? ? ? ? ? ? ? in: ?None
? ? ? ? ? ? ?out: ?None
? ? ? ? ? return: ?None
? ? ? ? ? others: ?Program Entry Func
? ? ? ? """
? ? ? ? while True:
? ? ? ? ? ? self.parse_html()
? ? ? ? ? ? # 不是最后一頁:pn-next
? ? ? ? ? ? # 最后一頁:pn-next disabled
? ? ? ? ? ? if self.driver.page_source.find("pn-next disabled") == -1: ?# 沒有找到 pn-next disabled,說明不是最后一頁
? ? ? ? ? ? ? ? self.driver.find_element(By.XPATH, '//*[@id="J_bottomPage"]/span[1]/a[9]').click()
? ? ? ? ? ? ? ? time.sleep(1)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? self.driver.quit()
? ? ? ? ? ? ? ? break
3.4 完整代碼
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class JDSpider:
? ? def __init__(self):
? ? ? ? # 設(shè)置為無界面
? ? ? ? self.options = webdriver.ChromeOptions() ?# 創(chuàng)建瀏覽器對象之前,創(chuàng)建options功能對象
? ? ? ? self.options.add_argument('--headless') ?# 添加無界面功能參數(shù)
? ? ? ? self.driver = webdriver.Chrome(options=self.options) ?# 構(gòu)造瀏覽器對象,打開瀏覽器
? ? ? ? self.driver.get(url="http://www.jd.com/") ?# 進(jìn)入主頁
? ? ? ? # 搜索框發(fā)送:python書籍,點(diǎn)擊搜索按鈕
? ? ? ? self.inputJD = self.driver.find_element(By.XPATH, '//*[@id="key"]') ?# 搜索框xpath://*[@id="key"]
? ? ? ? self.inputJD.send_keys("python書籍")
? ? ? ? self.driver.find_element(By.XPATH,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'//*[@id="search"]/div/div[2]/button').click() ?# 搜索按鈕xpath://*[@id="search"]/div/div[2]/button 并點(diǎn)擊
? ? ? ? time.sleep(1) ?# 要給頁面元素加載預(yù)留時(shí)間
? ? def parse_html(self):
? ? ? ? """
? ? ? ? function: ?具體提取數(shù)據(jù)方法
? ? ? ? ? ? ? in: ?None
? ? ? ? ? ? ?out: ?None
? ? ? ? ? return: ?None
? ? ? ? ? others: ?Data Extraction Func
? ? ? ? """
? ? ? ? self.driver.execute_script(
? ? ? ? ? ? 'window.scrollTo(0,document.body.scrollHeight)'
? ? ? ? ) ?# 先把滾動條拉到最底部,等待所有商品加載完成再進(jìn)行數(shù)據(jù)爬取
? ? ? ? time.sleep(3) ?# 給頁面元素加載預(yù)留時(shí)間
? ? ? ? # 具體提取數(shù)據(jù)
? ? ? ? li_list = self.driver.find_elements(By.XPATH,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '//*[@id="J_goodsList"]/ul/li') ?# 基準(zhǔn)xpath://*[@id="J_goodsList"]/ul/li 每一個(gè)商品對應(yīng)一個(gè)li節(jié)點(diǎn)
? ? ? ? item = {} ?# 定義一個(gè)空字典
? ? ? ? for li in li_list:
? ? ? ? ? ? item["名稱"] = li.find_element(By.XPATH, './/div[@class="p-name"]/a/em').text.strip()
? ? ? ? ? ? item["價(jià)格"] = li.find_element(By.XPATH, './/div[@class="p-price"]/strong').text.strip()
? ? ? ? ? ? item["評價(jià)"] = li.find_element(By.XPATH, './/div[@class="p-commit"]/strong').text.strip()
? ? ? ? ? ? item["商家"] = li.find_element(By.XPATH, './/div[@class="p-shopnum"]').text.strip()
? ? ? ? ? ? print(item) ?# 打印
? ? def run(self):
? ? ? ? """
? ? ? ? function: ?程序入口函數(shù)
? ? ? ? ? ? ? in: ?None
? ? ? ? ? ? ?out: ?None
? ? ? ? ? return: ?None
? ? ? ? ? others: ?Program Entry Func
? ? ? ? """
? ? ? ? while True:
? ? ? ? ? ? self.parse_html()
? ? ? ? ? ? # 不是最后一頁:pn-next
? ? ? ? ? ? # 最后一頁:pn-next disabled
? ? ? ? ? ? if self.driver.page_source.find("pn-next disabled") == -1: ?# 沒有找到 pn-next disabled,說明不是最后一頁
? ? ? ? ? ? ? ? self.driver.find_element(By.XPATH, '//*[@id="J_bottomPage"]/span[1]/a[9]').click()
? ? ? ? ? ? ? ? time.sleep(1)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? self.driver.quit()
? ? ? ? ? ? ? ? break
if __name__ == '__main__':
? ? spider = JDSpider()
? ? spider.run()
3.5 實(shí)現(xiàn)效果
?