pyspider示例代码一:利用phantomjs解决js问题
本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉。pyspider示例代码官方网站是http://demo.pyspider.org/。上面的示例代码太多,无从下手。因此本人找出一下比较经典的示例进行简单讲解,希望对新手有一些帮助。
示例说明:
如果页面中部分数据或文字由js生成,pyspider不能直接提取页面的数据。pyspider获取页面的代码,但是其中的js代码phantomjs,解决js代码执行问题。
使用方法:
方法一:在self.crawl函数中添加fetch_type="js"调用phantomjs执行js代码。
方法二:为函数添加参数@config(fetch_type="js")。
示例代码:
1、www.sciencedirect.com网站示例
#!/usr/bin/env python
# -*- encoding: utf- -*-
# vim: set et sw= ts= sts= ff=unix fenc=utf8:
# Created on -- :: import re
from libs.base_handler import * class Handler(BaseHandler):
'''
this is a sample handler
'''
crawl_config = {
"headers": {
"User-Agent": "BaiDu_Spider",
},
"timeout":,
"connect_timeout":
} def on_start(self):
self.crawl('http://www.sciencedirect.com/science/article/pii/S1568494612005741',timeout=,connect_timeout=,
callback=self.detail_page)
self.crawl('http://www.sciencedirect.com/science/article/pii/S0167739X12000581',timeout=,connect_timeout=,
age=, callback=self.detail_page)
self.crawl('http://www.sciencedirect.com/science/journal/09659978',timeout=,connect_timeout=,
age=, callback=self.index_page) @config(fetch_type="js")
def index_page(self, response):
for each in response.doc('a').items():
url=each.attr.href
#print(url)
if url!=None:
if re.match('http://www.sciencedirect.com/science/article/pii/\w+$', url):
self.crawl(url, callback=self.detail_page,timeout=,connect_timeout=) @config(fetch_type="js")
def detail_page(self, response):
self.index_page(response)
self.crawl(response.doc('#relArtList > li > .cLink').attr.href, callback=self.index_page,timeout=,connect_timeout=) return {
"url": response.url,
"title": response.doc('.svTitle').text(),
"authors": [x.text() for x in response.doc('.authorName').items()],
"abstract": response.doc('.svAbstract > p').text(),
"keywords": [x.text() for x in response.doc('.keyword span').items()],
}
pyspider示例代码一:利用phantomjs解决js问题的更多相关文章
- pyspider示例代码:解析JSON数据
pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下比较经典的示例进行简单讲解,希望对新手有一些帮助. 示例说明: py ...
- pyspider示例代码二:解析JSON数据
本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下 ...
- pyspider示例代码三:用PyQuery解析页面数据
本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一些 ...
- 爬虫:selenium + phantomjs 解决js抓取问题(一)
selenium模块主要用来做测试,模拟键盘.鼠标来操作浏览器. phantomjs 就像一个无界面的浏览器一样. 两个结合能很好的解决js抓取的问题. 测试代码: #coding=utf-8 fro ...
- pyspider示例代码五:实现自动翻页功能
实现自动翻页功能 示例代码一 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: v2ex fro ...
- pyspider示例代码六:传递参数
传递参数 示例一 #!/usr/bin/env python # -*- encoding: utf- -*- # vim: ts= sts= ff=unix fenc=utf8: # Created ...
- pyspider示例代码七:自动登陆并获得PDF文件下载地址
自动登陆并获得PDF文件下载地址 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: pdf_sp ...
- pyspider示例代码四:搜索引擎爬取
搜索引擎爬取 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: __git_lab_fix fr ...
- 利用PhantomJS进行网页截屏,完美解决截取高度的问题
关于PhantomJS PhantomJS 是一个基于WebKit的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS ...
随机推荐
- 0302 IT行业就业与软件工程
阅读以下文章 http://www.thea.cn/news/terminal/9/9389.html http://www.shzhidao.cn/system/2015/09/22/0102610 ...
- Reusing & Composing GraphQL APIs with GraphQL Bindings
With GraphQL bindings you can embed existing GraphQL APIs into your GraphQL server. In previous blog ...
- 在.NET中实现Actor模型的不同方式
上周,<实现领域驱动设计>(Implementing Domain-Driven Design)一书的作者Vaughn Vernon,发布了Dotsero,这是一个使用C#编写的.基于.N ...
- H3C 交换机基本设置(telnet、SSH、链路聚合)
http://www.h3c.com/cn/d_201710/1038172_30005_0.htm#_Toc493869056 H3C S5560S-SI&S5130S-SI[LI]& ...
- Weex入门与进阶指南
Weex入门与进阶指南 标签: WeexiOSNative 2016-07-08 18:22 59586人阅读 评论(8) 收藏 举报 本文章已收录于: iOS知识库 分类: iOS(87) 职 ...
- emacs之配置gtags
~/emacsConfig/gtags-setting.el (if (eq system-type 'darwin) (add-to-list 'load-path "/usr/local ...
- Linux嵌入式内核模块程序设计
1.环境搭建 vmware+Fedora 2.创建一个Hello文件 [root@localhost ~]# mkdir Hello 3.在Hello里面创建 hello.c 和 Makefile 两 ...
- postman-1版本区别、选择
postman基于乙醇在腾讯课堂的postman教程 postman特点: 1.便于开发:开发接口的时候需要快速的调用接口,以便调试 2.便于测试:测试的时候需要非常方便的调用接口,通过不同的参数去测 ...
- Ubuntu12.10下Python(pyodbc)访问SQL Server解决方案
一.基本原理 请查看这个网址,讲得灰常详细:http://www.jeffkit.info/2010/01/476/ 二.实现步骤 1.安装linux下SQL Server的驱动程序 安装Free ...
- ffmpeg重要的参考学习网址
http://lib.csdn.net/liveplay/knowledge/1586 FFmpeg滤镜使用指南 http://blog.csdn.net/fireroll/article/detai ...