(我喜歡雨天,因為雨天我可以回到童年踩水花!哈!)
2018年 --7月--12日 : 多雲又暴雨 T—T
前言
我要把爬蟲的終極利器介紹一下,這個隻要是我們肉眼能看到的,就算在源碼中或者在json中,或是post也無法得到的資料,我們都可以擷取到,雷同F12後源碼暴露在你面前一樣!
這次需要用到selenium家族的一些成員,它們各司其職,各個身懷絕技。
先介紹一下selenium子產品:Selenium is a suite of tools specifically for automating web browsers.(Selenium是一套專門用于自動化web浏覽器的工具。)
---:(這裡補充一下,在需要用到這種大型子產品的時候,一定要去讀官方文檔,不要去聽百度裡面和一些斷章取義的人瞎寫的内容,他們更容易把你帶偏。)
這個Selenium子產品主要是應對我們自動浏覽網頁資料所需要用的,讓程式來執行半智能,前提你要教會它去做什麼事情!
直接介紹此次所需要用到的家族子產品:
1 from selenium import webdriver
2 import time
3 from selenium.webdriver.common.keys import Keys
4 from selenium.webdriver.common.action_chains import ActionChains
5 from selenium.webdriver.common.by import By
一、每一個解釋一下哈,按順序對号:
1、主子產品的嵌入,主要是應對控制程式自動打開浏覽器浏覽網頁功能。
2、作為開發人員,尤其是對網頁自動化測試的開發工具必須需要time子產品來制約程式的通路時間,因為可能網站會直接把你IP封掉。
3、selenium 子產品家族成員Keys,此成員是應當以模拟鍵盤操作,應對模拟輸入使用者登入名和密碼,或者價值資料索引輸入。
4、selenium 子產品家族成員ActionChains,它則是應對模拟滑鼠操作,對與滑鼠的輕按兩下,單擊,左右鍵,應對我們翻頁,搜尋按鈕的點選功能。
5、selenium 子產品家族成員By,這個則是我們要教會它所要做的事情,也是我們資料挖掘又要用到的核心價值功能之一,應對價值資料抓取。
二、開發初步:
1、操作程式打開浏覽器并打開我們需要進入的網頁:
1 url = 'https://www.xxx.com'
2 driver=webdriver.Chrome()
3 driver.get(url)
4 time.sleep(5)
5 driver.quit()
這裡可以自己測試一下,我所使用的是Google的浏覽器,你們可以嘗試使用Firefox,他們有一些的差別,主要是站點的差別!
2、進入頁面後鎖定tag
html:
1 <div id="aaa" class="bbb" name="ccc">
2 <p></p>
3 <p><a></p>
4 </div>
python:
1 element = driver.find_element_by_id("aaa")
2 frame = driver.find_element_by_tag_name("div")
3 cheese = driver.find_element_by_name("ccc")
4 cheeses = driver.find_elements_by_class_name("bbb")
5
6 or
7
8 from selenium.webdriver.common.by import By
9 element = driver.find_element(by=By.ID, value="aaa")
10 frame = driver.find_element(By.TAG_NAME, "div")
11 cheese = driver.find_element(By.NAME, "ccc")
12 cheeses = driver.find_elements(By.CLASS_NAME, "bbb")
這裡每一個都是鎖定tag樹,它們都是根據id,class,name,tagname來定義的。
1 xpath_class = driver.find_element_by_xpath('//div[@class="bbb"]/p')
2 xpath_id = driver.find_element_by_xpath('//div[@id="aaa"]/p')
這是通用方法的,Xpath方法,它們都輸屬于解析網頁的内容鎖定tag。
3、處理操作:
當我們鎖定功能鍵的tag屬性的時候,我們就可以進一步操作,比如換頁,搜尋功能的實作,對于模拟鍵盤輸入的可以參考我的另一篇部落格,
《python自動化爬蟲》這裡我們就介紹一下模拟滑鼠的操作:
1 elem = driver.find_element_by_xpath('//a[@id="tagname"]')
2 ActionChains(driver).double_click(elem).perform()
3 time.sleep(3)
因為時間問題,我隻是介紹一下滑鼠左鍵單擊換頁操作,其他的何以參考一下官方文檔:
Selenium WebdriveActionChains:鎖定浏覽器,double_click鎖定tag标簽樹,.perform():點選标簽樹
4、擷取價值資料
這裡的操作類似與Xpath的文法:
driver.find_elements_by_tag_name('td')[3].text
driver.find_elements_by_tag_name('a').get_attribute('href')
這裡注意一下elements,指所有的tag-> a比标簽的href,這裡是list格式,需要周遊。
5、最後來一串完整代碼:
1 from selenium import webdriver
2 import time
3 import lxml.html as HTML
4 from bs4 import BeautifulSoup
5 from selenium.webdriver.common.keys import Keys
6 from selenium.webdriver.common.action_chains import ActionChains
7 from pymongo import MongoClient,ASCENDING, DESCENDING
8 from selenium.webdriver.common.by import By
9 def parser():
10 url = 'https://www.xxx.com'
11 driver=webdriver.Chrome()
12 driver.get(url)
13 time.sleep(5)
14 for i in range(1,675):
15 a = driver.find_element_by_xpath('//div[@class="aaa"]')
16 tr = a.find_elements_by_tag_name('tr')
17 for j in xrange(1,len(tr)):
18 quantity = tr[j].find_elements_by_tag_name('td')[3].text
19 producturl = tr[j].find_elements_by_tag_name('td')[0].find_elements_by_tag_name("div")[1].find_element_by_tag_name('ul').find_element_by_tag_name('li').find_element_by_tag_name('a').get_attribute('href')
20 producturl_db(producturl,quantity)
21 elem = driver.find_element_by_xpath('//a[@id="eleNextPage"]')
22 ActionChains(driver).double_click(elem).perform()
23 time.sleep(3)
24
25 driver.quit()
selenium有個小GUB,就是在用Xpath的時候,你已經找到父級tag,但是這個父級很多,比如tr,你如果周遊它,尋找td的話,那麼你還是使用find_elements_by_tag_name,因為那個會初始化,不會管你找到那個父級。是以這裡是需要注意的!
最後祝你們加油!!!!!
Welcome to Python world! I have a contract in this world! How about you?