溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用python進(jìn)行數(shù)據(jù)挖掘

發(fā)布時間:2020-11-09 11:16:41 來源:億速云 閱讀:245 作者:小新 欄目:編程語言

小編給大家分享一下如何使用python進(jìn)行數(shù)據(jù)挖掘,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

直接介紹此次所需要用到的家族模塊:

1 from selenium import webdriver2 import time3 from selenium.webdriver.common.keys import Keys4 from selenium.webdriver.common.action_chains import ActionChains5 from selenium.webdriver.common.by import By

一、每一個解釋一下哈,按順序?qū)μ枺?/span>

1、主模塊的嵌入,主要是應(yīng)對控制程序自動打開瀏覽器瀏覽網(wǎng)頁功能。

2、作為開發(fā)人員,尤其是對網(wǎng)頁自動化測試的開發(fā)工具必須需要time模塊來制約程序的訪問時間,因為可能網(wǎng)站會直接把你IP封掉。

3、selenium 模塊家族成員Keys,此成員是應(yīng)當(dāng)以模擬鍵盤操作,應(yīng)對模擬輸入用戶登錄名和密碼,或者價值數(shù)據(jù)索引輸入。

4、selenium 模塊家族成員ActionChains,它則是應(yīng)對模擬鼠標(biāo)操作,對與鼠標(biāo)的雙擊,單擊,左右鍵,應(yīng)對我們翻頁,搜索按鈕的點擊功能。

5、selenium 模塊家族成員By,這個則是我們要教會它所要做的事情,也是我們數(shù)據(jù)挖掘又要用到的核心價值功能之一,應(yīng)對價值數(shù)據(jù)抓取。

二、開發(fā)初步:

1、操作程序打開瀏覽器并打開我們需要進(jìn)入的網(wǎng)頁:

1 url = 'https://www.xxx.com'2 driver=webdriver.Chrome()3 driver.get(url)4 time.sleep(5)5 driver.quit()

這里可以自己測試一下,我所使用的是Google的瀏覽器,你們可以嘗試使用Firefox,他們有一些的區(qū)別,主要是站點的區(qū)別!

2、進(jìn)入頁面后鎖定tag

html:

1 <div id="aaa" 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樹,它們都是根據(jù)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方法,它們都輸屬于解析網(wǎng)頁的內(nèi)容鎖定tag。

3、處理操作:

當(dāng)我們鎖定功能鍵的tag屬性的時候,我們就可以進(jìn)一步操作,比如換頁,搜索功能的實現(xiàn)

這里我們就介紹一下模擬鼠標(biāo)的操作:

1 elem = driver.find_element_by_xpath('//a[@id="tagname"]')2 ActionChains(driver).double_click(elem).perform()3 time.sleep(3)

因為時間問題,我只是介紹一下鼠標(biāo)左鍵單擊換頁操作,其他的何以參考一下官方文檔:Selenium Webdrive

ActionChains:鎖定瀏覽器,double_click鎖定tag標(biāo)簽樹,.perform():點擊標(biāo)簽樹

4、獲取價值數(shù)據(jù)

這里的操作類似與Xpath的語法:

driver.find_elements_by_tag_name('td')[3].text
driver.find_elements_by_tag_name('a').get_attribute('href')

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].text19 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()

看完了這篇文章,相信你對如何使用python進(jìn)行數(shù)據(jù)挖掘有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI