爬取动态js html数据方法二 使用phantomjs

news/2024/7/19 11:33:47 标签: 爬虫, phantomjs

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-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
# Created on 2014-10-31 13:05:52

import re
from libs.base_handler import *

class Handler(BaseHandler):
    '''
    this is a sample handler
    '''
    crawl_config = {
        "headers": {
            "User-Agent": "BaiDu_Spider",
        },
        "timeout":300,
        "connect_timeout":100
    }
    
    def on_start(self):
        self.crawl('http://www.sciencedirect.com/science/article/pii/S1568494612005741',timeout=300,connect_timeout=100,
                   callback=self.detail_page)
        self.crawl('http://www.sciencedirect.com/science/article/pii/S0167739X12000581',timeout=300,connect_timeout=100,
                   age=0, callback=self.detail_page)
        self.crawl('http://www.sciencedirect.com/science/journal/09659978',timeout=300,connect_timeout=100,
                   age=0, 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=300,connect_timeout=100)
        
    @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=300,connect_timeout=100)
        
        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的方法

http://www.niftyadmin.cn/n/1095148.html

相关文章

keytool创建Keystore和Trustsotre文件

一、生成一个含有一个私钥的keystore文件 userae01:~$ keytool -genkey -keystore keystore -alias jetty-azkaban -keyalg RSA Enter keystore password: Re-enter new password: What is your first and last name?[Unknown]: azkaban What is the name of your organiza…

知识图谱入门 (四) 知识挖掘

欢迎大家关注我的博客 http://pelhans.com/ ,所有文章都会第一时间发布在那里哦~ 本节介绍了知识挖掘的相关技术,包含实体链接与消歧,知识规则挖掘,知识图谱表示学习。 知识挖掘 知识挖掘是指从数据中获取实体及新的实体链接和新…

MySQL 忘记密码后的重置操作

一、修改配置文件方式 1、关闭 MySQL linux: 1)service mysqld stop 2)/usr/local/mysql/support-files/mysql.server stop Windows: …

pyspider打开url看不到内容

示例:未使用phantomjs前:数据部分为空白url列表:使用phantomjs:url列表:遇到这类动态js封装数据的情况,建议使用phantomjs

带cookie验证解决方法

示例: 不带cookie:带cookie结果;分析cookie:示例无论是什么设备,第一次访问该站,都会弹出一个521的错误状态码,与此同时还会返回一个Cookie。 浏览器接受到状态码与Cookie,会再次进行一次请求&a…

Andorid Dialog 显示宽度设置

dialog.getWindow().setLayout(200, LayoutParams.WRAP_CONTENT);转载于:https://www.cnblogs.com/daxin/p/5057429.html

让图片任意旋转

前几天做了一个让图片旋转任意角度的功能,今天跟大家分享一下。。 1、首先把力图片加载进来。 //strPagePath为图片的路径System.Drawing.Image ImgPointer null;if (File.Exists(strPagePath))ImgPointer System.Drawing.Image.FromFile(strPagePath);//加载图片…

爬虫iframe blocked解决

pyspider爬虫遇到iframe blocked的时候;需要绕过iframe跨域问题,直接爬取iframe框架url:上边为iframe框架封的数据,我们找到iframe框架内的url,直接爬取该url数据:爬取该url数据:解决iframe的问题&#xff…