本系列文章主要记录和讲解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问题的更多相关文章

  1. pyspider示例代码:解析JSON数据

    pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下比较经典的示例进行简单讲解,希望对新手有一些帮助. 示例说明: py ...

  2. pyspider示例代码二:解析JSON数据

    本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下 ...

  3. pyspider示例代码三:用PyQuery解析页面数据

    本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一些 ...

  4. 爬虫:selenium + phantomjs 解决js抓取问题(一)

    selenium模块主要用来做测试,模拟键盘.鼠标来操作浏览器. phantomjs 就像一个无界面的浏览器一样. 两个结合能很好的解决js抓取的问题. 测试代码: #coding=utf-8 fro ...

  5. pyspider示例代码五:实现自动翻页功能

    实现自动翻页功能 示例代码一 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: v2ex fro ...

  6. pyspider示例代码六:传递参数

    传递参数 示例一 #!/usr/bin/env python # -*- encoding: utf- -*- # vim: ts= sts= ff=unix fenc=utf8: # Created ...

  7. pyspider示例代码七:自动登陆并获得PDF文件下载地址

    自动登陆并获得PDF文件下载地址 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: pdf_sp ...

  8. pyspider示例代码四:搜索引擎爬取

    搜索引擎爬取 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: __git_lab_fix fr ...

  9. 利用PhantomJS进行网页截屏,完美解决截取高度的问题

    关于PhantomJS PhantomJS 是一个基于WebKit的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS ...

随机推荐

  1. 《DSP using MATLAB》第2章习题Problem2.1

    1.代码: %% ------------------------------------------------------------------------ %% Output Info abo ...

  2. cratedb json 数据导入

    基本环境的搭建,可以参考相关文档,或者直接使用docker 安装 docker run -d -p 4200:4200 crate 导出mongodb数据(可选,同时使用工具进行数据类型转换) mon ...

  3. ambassador 学习六 Module说明

    模块允许给与特定的mapping 或者整体添加特定的行为,方便进行系统的控制. 当前的module 定义主要是系统级别的 当前系统主要的配置 --- apiVersion: ambassador/v0 ...

  4. web应用中的Filter过滤器之基础概述

    1 过滤器概述 当web容器接收到对一个资源的请求时,它将判断是否有过滤器与这个资源相关联,如果有,那么容器将把这个请求交给过滤器进行处理.在过滤器中,你可以改变请求的内容或者重新设置请求的报头信息, ...

  5. 【转】Vim自动补全插件----YouCompleteMe安装与配置

    原文网址:http://www.cnblogs.com/zhongcq/p/3630047.html 使用Vim编写程序少不了使用自动补全插件,在Linux下有没有类似VS中的Visual Assis ...

  6. 为eclipse安装python、shell开发环境和SVN插件

    http://www.crazyant.net/1185.html 为eclipse安装python.shell开发环境和SVN插件 2013/08/27 by Crazyant 暂无评论 eclip ...

  7. 流行的FPGA的上电复位

    在实际设计中,由于外部阻容复位时间短,可能无法使FPGA内部复位到理想的状态,所以今天介绍一下网上流行的复位逻辑. 在基于verilog的FPGA设计中,我们常常可以看到以下形式的进程: 信号rst_ ...

  8. Keil for ARM与C++

    1. 如果你的程序中使用了C++全局变量,那么*不要*使用MicroLIB,否则Keil会说某某Symbol找不到 2. 不使用MicroLIB带来的一个问题是KEIL会使用semihosting S ...

  9. maven settings.xml 文件

    指定jdk 的版本: <profile> <id>jdk-1.8</id> <activation> <activeByDefault>tr ...

  10. leetcode414

    public class Solution { public int ThirdMax(int[] nums) { ; var list = nums.Distinct().OrderByDescen ...