接着上一遍,在用Selenium+phantomjs 抓取数据过程中发现,有时候抓取不到,所以又测试了用Selenium+浏览器驱动的方式:具体代码如下:

#coding=utf-8
import os
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
import IniFile
class IEDriverCrawler: def __init__(self):
#通过配置文件获取IEDriverServer.exe路径
configfile = os.path.join(os.getcwd(),'config.conf')
cf = IniFile.ConfigFile(configfile)
IEDriverServer = cf.GetValue("section", "IEDriverServer")
#每抓取一页数据延迟的时间,单位为秒,默认为5秒
self.pageDelay = 5
pageInteralDelay = cf.GetValue("section", "pageInteralDelay")
if pageInteralDelay:
self.pageDelay = int(pageInteralDelay) os.environ["webdriver.ie.driver"] = IEDriverServer
self.driver = webdriver.Ie(IEDriverServer) def CatchData(self,id,firstUrl,nextUrl,restUrl):
'''
抓取数据
:param id: 要获取元素标签的ID
:param firstUrl: 首页Url
:param nextUrl: 下一页URL
:param restUrl: 下一页URL的组成部分
:return:
'''
#加载首页
self.driver.get(firstUrl)
#打印标题
print self.driver.title
# id = "J_albumFlowCon"
element = self.driver.find_element_by_id(id)
txt = element.text.encode('utf8')
#打印获取的信息
print txt
print ' '
time.sleep(20) # 延迟20秒,
#由于有多页数据,为了测试,只取出几页数据
for i in range(2, 4):
print ' '
time.sleep(20) # 延迟20秒,
url = nextUrl + str(i) + restUrl
self.driver.get(url)
element = self.driver.find_element_by_id(id)
txt = element.text.encode('utf8')
print txt
self.driver.close()
self.driver.quit() def CatchDatabyClickNextButton(self,id,firstUrl):
'''
抓取数据
:param id: 要获取元素标签的ID
:param firstUrl: 首页Url
:return:
'''
start = time.clock()
#加载首页
self.driver.get(firstUrl)
#打印标题
print self.driver.title
# id = "J_ItemList"
firstPage = self.driver.find_element_by_id(id)
txt = firstPage.text.encode('utf8')
self.printTxt(1,txt) #获取总页数
name = 'filterPageForm'
totalPageElement = self.driver.find_element_by_name(name)
txt = totalPageElement.text.encode('utf8')#ui-page-next
pattern = re.compile(r'\d+')
flist = re.findall(pattern, txt)
pageCount = 1
if flist and len(flist)>0:
pageCount = int(flist[0])
if pageCount > 1:
pageCount = 10 #先爬三页
for index in range(2,pageCount + 1):
time.sleep(self.pageDelay) #延迟五秒
nextElement = self.driver.find_element_by_xpath("//a[@class='ui-page-next']")
nextUrl = nextElement.get_attribute('href')
self.driver.get(nextUrl)
# ActionChains(self.driver).click(element)
dataElement = self.driver.find_element_by_id(id)
txt = dataElement.text.encode('utf8') # ui-page-next
print ' '
self.printTxt(index, txt) self.driver.close()
self.driver.quit()
end = time.clock()
print ' '
print "抓取每页数据后延迟 %d 秒" % self.pageDelay
print "总共抓取了 %d页数据" % pageCount
print "整个过程用时间: %f 秒" % (end - start) def printTxt(self,pageIndex,stringTxt):
'''
打印抓取的每页数据
:param pageIndex:页数
:param stringTxt:每页抓取的数据
:return:
'''
if stringTxt.find('¥') > -1:
itemList = stringTxt.split('¥')
print '第' + str(pageIndex) + '页数据'
print ' '
for item in itemList:
if len(item) > 0:
its = item.split('\n')
if len(its)>=4:
print '单价: ¥%s' % its[0]
print '品牌: %s' % its[1]
print '销售店铺名称: %s' % its[2]
print '成交量: %s' % its[3]
print ' ' #测试抓取淘宝数据
# obj = IEDriverCrawler()
# firstUrl = "https://ai.taobao.com/search/index.htm?pid=mm_26632323_6762370_25910879&unid=&source_id=search&key=%E6%89%8B%E6%9C%BA&b=sousuo_ssk&clk1=&prepvid=200_11.251.246.148_396_1490081427029&spm=a231o.7712113%2Fa.a3342.1"
# nextUrl='https://ai.taobao.com/search/index.htm?pid=mm_26632323_6762370_25910879&unid=&source_id=search&key=%E6%89%8B%E6%9C%BA&b=sousuo_ssk&clk1=&prepvid=200_11.251.246.157_19825_1490081412211&spm=a231o.7076277.1998559105.1&page='
# # url='https://ai.taobao.com/search/index.htm?pid=mm_26632323_6762370_25910879&unid=&source_id=search&key=%E6%89%8B%E6%9C%BA&b=sousuo_ssk&clk1=&prepvid=200_11.251.246.148_396_1490081427029&spm=a231o.7712113%2Fa.a3342.1&page=2&pagesize=120'
# # url='https://ai.taobao.com/search/index.htm?pid=mm_26632323_6762370_25910879&unid=&source_id=search&key=%E6%89%8B%E6%9C%BA&b=sousuo_ssk&clk1=&prepvid=200_11.251.246.148_396_1490081427029&spm=a231o.7712113%2Fa.a3342.1&page=3&pagesize=120'
# restUrl = '&pagesize=120'
# obj.CatchData("J_albumFlowCon",firstUrl,nextUrl,restUrl) #测试抓取天猫数据
obj = IEDriverCrawler()
firstUrl = "https://list.tmall.com/search_product.htm?q=%CA%D6%BB%FA&type=p&vmarket=&spm=875.7931836%2FB.a2227oh.d100&from=mallfp..pc_1_searchbutton"
obj.CatchDatabyClickNextButton("J_ItemList",firstUrl) 本文章仅仅作为交流。

[Python爬虫] 之三:Selenium 调用IEDriverServer 抓取数据的更多相关文章

  1. python爬虫之分析Ajax请求抓取抓取今日头条街拍美图(七)

    python爬虫之分析Ajax请求抓取抓取今日头条街拍美图 一.分析网站 1.进入浏览器,搜索今日头条,在搜索栏搜索街拍,然后选择图集这一栏. 2.按F12打开开发者工具,刷新网页,这时网页回弹到综合 ...

  2. python爬虫构建代理ip池抓取数据库的示例代码

    爬虫的小伙伴,肯定经常遇到ip被封的情况,而现在网络上的代理ip免费的已经很难找了,那么现在就用python的requests库从爬取代理ip,创建一个ip代理池,以备使用. 本代码包括ip的爬取,检 ...

  3. Python爬虫初探 - selenium+beautifulsoup4+chromedriver爬取需要登录的网页信息

    目标 之前的自动答复机器人需要从一个内部网页上获取的消息用于回复一些问题,但是没有对应的查询api,于是想到了用脚本模拟浏览器访问网站爬取内容返回给用户.详细介绍了第一次探索python爬虫的坑. 准 ...

  4. Python爬虫入门教程 46-100 Charles抓取手机收音机-手机APP爬虫部分

    1. 手机收音机-爬前叨叨 今天选了一下,咱盘哪个APP呢,原计划是弄荔枝APP,结果发现竟然没有抓到数据,很遗憾,只能找个没那么圆润的了.搜了一下,找到一个手机收音机 下载量也是不错的. 2. 爬虫 ...

  5. Python爬虫入门教程 45-100 Charles抓取兔儿故事-下载小猪佩奇故事-手机APP爬虫部分

    1. Charles抓取兔儿故事背景介绍 之前已经安装了Charles,接下来我将用两篇博客简单写一下关于Charles的使用,今天抓取一下兔儿故事里面关于小猪佩奇的故事. 爬虫编写起来核心的重点是分 ...

  6. python requests 模拟登陆网站,抓取数据

    抓取页面数据的时候,有时候我们需要登陆才可以获取页面资源,那么我们需要登陆以后才可以跳转到对应的资源页面,那么我们需要通过模拟登陆,登陆成功以后再次去抓取对应的数据. 首先我们需要通过手动方式来登陆一 ...

  7. Python爬虫系列-Selenium+Chrome/PhantomJS爬取淘宝美食

    1.搜索关键字 利用Selenium驱动浏览器搜索关键字,得到查询后的商品列表 2.分析页码并翻页 得到商品页码数,模拟翻页,得到后续页面的商品列表 3.分析提取商品内容 利用PyQuery分析源码, ...

  8. Python开发爬虫之动态网页抓取篇:爬取博客评论数据——通过Selenium模拟浏览器抓取

    区别于上篇动态网页抓取,这里介绍另一种方法,即使用浏览器渲染引擎.直接用浏览器在显示网页时解析 HTML.应用 CSS 样式并执行 JavaScript 的语句. 这个方法在爬虫过程中会打开一个浏览器 ...

  9. Python爬虫之三种网页抓取方法性能比较

    下面我们将介绍三种抓取网页数据的方法,首先是正则表达式,然后是流行的 BeautifulSoup 模块,最后是强大的 lxml 模块. 1. 正则表达式   如果你对正则表达式还不熟悉,或是需要一些提 ...

随机推荐

  1. Nodejs 接收RabbitMQ消息

    参考官方地址:https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html 关于C#消息发送端,请参考<c# RabbitMQ ...

  2. vue引入Vue-Awesome

    Vue-Awesome建立在Font Awesome上 v4.5.0,取决于Vue.js v2.0.1 +,所以安装了Vue-Awesome就可以直接使用Font Awesome的字体图标. 1.安装 ...

  3. 如何解决pytorch 编译时CUDA版本与运行时CUDA版本不对应

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 如何解决pytorch 编译时CUDA版本与运行时CUDA版本不对应 如果pytorch的编译时 ...

  4. Django+Nginx+uwsgi搭建自己的博客(一)

    最近对写爬虫有些厌倦了,于是将方向转移到了Web开发上.其实在之前自己也看过一部分Flask的资料,但总觉得Flask的资料有些零散,而且需要的各种扩展也非常多.因此,我将研究方向转移到了另一个主流的 ...

  5. 洛谷——P1349 广义斐波那契数列

    题目描述 广义的斐波那契数列是指形如an=p*an-1+q*an-2的数列.今给定数列的两系数p和q,以及数列的最前两项a1和a2,另给出两个整数n和m,试求数列的第n项an除以m的余数. 输入输出格 ...

  6. New Year Tree 【DFS序+线段树区间查询修改+二进制保存状态】

    题目链接[http://codeforces.com/problemset/problem/620/E] 题意:给出n个数,每个数有一个初始的颜色.由这n个数组成一颗树.有两种操作1.将以节点u为根的 ...

  7. 【UOJ 117】欧拉回路

    #117. 欧拉回路 有一天一位灵魂画师画了一张图,现在要你找出欧拉回路,即在图中找一个环使得每条边都在环上出现恰好一次. 一共两个子任务: 这张图是无向图.(50分) 输入格式 第一行一个整数 t, ...

  8. 2017haoi总结

    暴力都写不对的蒟蒻QAQ 现在只看了上午的第二题..   AM.T2 写了40分的记忆化搜索,最差复杂度大概是n^3,100以下应该是稳过的..通过递归返回[l+1,r]的答案,l=r特判,int函数 ...

  9. 冒泡排序 Exercise07_18

    import java.util.Arrays; import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:冒泡排序 * */ pu ...

  10. 【8.22校内测试】【数学】【并查集】【string】

    今天的t2t3能打出来80分的暴力都好满足啊QwQ.(%%%$idy$ 今天的签到题,做的时候一眼就看出性质叻qwq.大于11的所有数分解合数都可以用4.6.9表示,乱搞搞就可以了. #include ...