前言
首先咱們先看看舔狗的卑微經歷吧
看看這卑微的聊天記錄,就是那種你發十句,別人不稀得回一句的那種,雖然是舔狗吧,但也玩出花嗎,最近剛發現了一個舔狗網站,來看看怎么把舔狗玩出花吧
分析頁面
其實很簡單的一個頁面,總的流程就是實現點擊換頁,然后進行文本的獲取
先來看看頁面
主要就是兩步,通過xpath獲取到文本和按鈕,然后進行分別操作,先來寫一下xpath
因為整個頁面只有一個按鈕標簽,所以獲取按鈕的xpath很簡單,如下圖所示
然后來獲取文本內容,與按鈕相同,該頁面中也只有一個article標簽,所以很容易寫xpath,如下圖,很簡單就拿到了
這樣其實就完成了,然后接下來就可以開始寫代碼了
主要代碼
selenium偽裝
url = 'https://www.nihaowua.com/dog.html'
# 躲避智能檢測
option = webdriver.ChromeOptions()
# option.headless = True
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=option)
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
{'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})
driver.get(url)
獲取文本內容
text = driver.find_element(By.XPATH, '//article')
print(text.text)
獲取按鈕並點擊
button = driver.find_element(By.XPATH, '//button')
button.click()
完整代碼
url = 'https://www.nihaowua.com/dog.html'
# 躲避智能檢測
option = webdriver.ChromeOptions()
# option.headless = True
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=option)
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
{'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})
driver.get(url)
i = 0;
while 1:
text = driver.find_element(By.XPATH, '//article')
print(text.text)
value = text.text
button = driver.find_element(By.XPATH, '//button')
button.click()
time.sleep(1)
driver.switch_to.window(driver.window_handles[0])
成果
這光看不存好像不大符合我這白嫖心理,就把這些內容存在數據庫里,有需要sql文件的xdm可以私信我
總之,蠻簡單的一個小爬蟲,沒事的時候寫寫,防止自己忘了基礎使用
僅供學習,侵權必刪
推薦下自己的爬蟲專欄,都是一些入門的爬蟲樣例,有興趣的兄弟們可以來看看,順便點一手關注
❤️爬蟲專欄,快來點我呀❤️
另外還有博主的爬蟲博客目錄,進來你才知道對你有沒有用,來看看吧
爬蟲樣例匯總,快來看看吧