本人刚才开始学习爬虫,从网上查询资料,写了一个利用Selenium+Phantomjs动态获取网站数据信息的例子,当然首先要安装Selenium+Phantomjs,具体的看

http://www.cnblogs.com/shaosks/p/6526817.html  Selenium下载: https://pypi.python.org/pypi/selenium/

phantomjs使用参考:http://javascript.ruanyifeng.com/tool/phantomjs.html 及官网:http://phantomjs.org/quick-start.html

源代码如下:

# coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.action_chains import ActionChains
import time
import re
import os class Crawler:
def __init__(self, firstUrl = "https://list.jd.com/list.html?cat=9987,653,655",
nextUrl = "https://list.jd.com/list.html?cat=9987,653,655&page=%d&sort=sort_rank_asc&trans=1&JL=6_0_0#J_main"):
self.firstUrl = firstUrl
self.nextUrl = nextUrl def getDetails(self,pageIndex,id = "plist"):
'''
获取详细信息
:param pageIndex: 页索引
:param id: 标签对应的id
:return:
'''
element = self.driver.find_element_by_id(id)
txt = element.text.encode('utf8')
items = txt.split('¥') for item in items:
if len(item) > 0:
details = item.split('\n')
print '¥' + item
# print '单价:¥'+ details[0]
# print '品牌:' + details[1]
# print '参与评价:' + details[2]
# print '店铺:' + details[3]
print ' '
print '第 ' + str(pageIndex) + '页' def CatchData(self,id = "plist",totalpageCountLable = "//span[@class='p-skip']/em/b"):
'''
抓取数据
:param id:获取数据的标签id
:param totalpageCountLable:获取总页数标记
:return:
'''
start = time.clock()
self.driver = webdriver.PhantomJS()
wait = ui.WebDriverWait(self.driver, 10)
self.driver.get(self.firstUrl)
#在等待页面元素加载全部完成后才进行下一步操作
wait.until(lambda driver: self.driver.find_element_by_xpath(totalpageCountLable))
# 获取总页数
pcount = self.driver.find_element_by_xpath(totalpageCountLable)
txt = pcount.text.encode('utf8')
print '总页数:' + txt
print '第1页'
print ' '
pageNum = int(txt)
pageNum = 3 # 只执行三次
i = 2
while (i <= pageNum):
self.getDetails(i,id)
print ' '
time.sleep(5) # 延迟5秒,防止获取数据过快而被封IP
wait = ui.WebDriverWait(self.driver, 10)
self.driver.get(self.nextUrl % i)
# driver.find_element_by_id("submit").click()
i = i + 1
else:
print 'Load Over'
end = time.clock()
print "Time: %f s" % (end - start) def main():
# 首页的url
firstUrl = "https://list.jd.com/list.html?cat=9987,653,655"
#下一页的url
nextUrl = "https://list.jd.com/list.html?cat=9987,653,655&page=%d&sort=sort_rank_asc&trans=1&JL=6_0_0#J_main"
cw = Crawler(firstUrl, nextUrl)
#总页数标签
totalpageCountLable = "//span[@class='p-skip']/em/b"
#获取数据的标签ID
id = "plist"
cw.CatchData(id,totalpageCountLable)
#测试
main() 参考:http://blog.csdn.net/eastmount/article/details/47907341

[Python爬虫] 之一 : Selenium+Phantomjs动态获取网站数据信息的更多相关文章

  1. [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论

    前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...

  2. Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容

    1,引言 在Python网络爬虫内容提取器一文我们详细讲解了核心部件:可插拔的内容提取器类gsExtractor.本文记录了确定gsExtractor的技术路线过程中所做的编程实验.这是第二部分,第一 ...

  3. Python爬虫 使用selenium处理动态网页

    对于静态网页,使用requests等库可以很方便的得到它的网页源码,然后提取出想要的信息.但是对于动态网页,情况就要复杂很多,这种页面的源码往往只有一个框架,其内容都是由JavaScript渲染出来的 ...

  4. [Python爬虫] :Selenium +phantomjs 利用 pyquery抓取脚本链接对应的内容

    抓取上面对应链接的网页的文章的内容 ele = element.attr("onclick") self.driver.execute_script(ele) sub_seleni ...

  5. Python爬虫(二十)_动态爬取影评信息

    本案例介绍从JavaScript中采集加载的数据.更多内容请参考:Python学习指南 #-*- coding:utf-8 -*- import requests import re import t ...

  6. Python爬虫教程-26-Selenium + PhantomJS

    Python爬虫教程-26-Selenium + PhantomJS 动态前端页面 : JavaScript: JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持 ...

  7. [Python爬虫]使用Selenium操作浏览器订购火车票

    这个专题主要说的是Python在爬虫方面的应用,包括爬取和处理部分 [Python爬虫]使用Python爬取动态网页-腾讯动漫(Selenium) [Python爬虫]使用Python爬取静态网页-斗 ...

  8. Python 爬虫利器 Selenium 介绍

    Python 爬虫利器 Selenium 介绍 转 https://mp.weixin.qq.com/s/YJGjZkUejEos_yJ1ukp5kw 前面几节,我们学习了用 requests 构造页 ...

  9. Python爬虫之selenium库使用详解

    Python爬虫之selenium库使用详解 本章内容如下: 什么是Selenium selenium基本使用 声明浏览器对象 访问页面 查找元素 多个元素查找 元素交互操作 交互动作 执行JavaS ...

随机推荐

  1. ref:PHP代码注入审计

    ref:https://www.waitalone.cn/php-code-injection.html 通俗易懂,全面清晰. 0x1 前言 为了方便自己以后的翻阅和查找,最近正在整理一些所学的内容. ...

  2. Linux内存管理中的slab分配器

    转载自:http://edsionte.com/techblog/archives/4019 Linux内核中基于伙伴算法实现的分区页框分配器适合大块内存的请求,它所分配的内存区是以页框为基本单位的. ...

  3. 爱奇艺全国高校算法大赛初赛B

    二分,验证. 二分一下答案,然后验证一下统计个数能否满足即可. #include <cstdio> #include <cmath> #include <cstring& ...

  4. RxSwift 系列(一)

    为什么使用RxSwift? 我们编写的代码绝大多数都涉及对外部事件的响应.当用户点击操作时,我们需要编写一个@IBAction事件来响应.我们需要观察通知,以检测键盘何时改变位置.当网络请求响应数据时 ...

  5. Ubuntu下查看软件版本及安装位置

    查看软件版本: XXX --version 或 aptitude show xxx 也可用apt-show-versions (要先安装sudo apt-get install apt-show-ve ...

  6. poj 1743 后缀数组 求最长不重叠重复子串

    题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. “主题”是整个音符序列的一个子串,它需要满足如下条件:1 ...

  7. python输入输出入门 A+B

    描述 求两个整数之和. 输入 输入数据只包括两个整数A和B. 输出 两个整数的和. 样例输入 1 2 样例输出  3 a=input().split() print(int(a[0])+int(a[1 ...

  8. 高斯消元法求解异或方程组: cojs.tk 539.//BZOJ 1770 牛棚的灯

    高斯消元求解异或方程组: 比较不错的一篇文章:http://blog.sina.com.cn/s/blog_51cea4040100g7hl.html cojs.tk  539. 牛棚的灯 ★★☆   ...

  9. Unity UGUI之Text

    下图是Text组件的内容. Character(字符) Text--输入要显示的文本 Font--要渲染文本的字体类型(例如:黑体.宋体) FontStyle--是否要加粗,倾斜等. Normal-- ...

  10. python gensim的第一次试用

    参考于 http://blog.csdn.net/xiaoquantouer/article/details/53583980 有一个地方很重要,一定要安装anaconda,安装库简直不要太方便. 先 ...