python怎么进行数据挖掘

833次阅读
没有评论
python怎么进行数据挖掘

想必没有比python更好用的大数据分析编程语言了吧,我们经常能看到很多数据分析里都是python,而且python也是大家公认的呢,下面给大家介绍,关于python的数据内容,一起来看下吧~

直接介绍此次所需要用到的家族模块:

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

一、每一个解释一下哈,按顺序对号:

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" 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属性的时候,我们就可以进一步操作,比如换页,搜索功能的实现

这里我们就介绍一下模拟鼠标的操作:

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

因为时间问题,我只是介绍一下鼠标左键单击换页操作,其他的何以参考一下官方文档:Selenium Webdrive

ActionChains:锁定浏览器,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')

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()

好了,大家可以跟着以上代码套用去编程哦。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-06-04发表,共计3076字。
新手QQ群:570568346,欢迎进群讨论 Python51学习