0

0

selenium和casperjs2种数据抓取方式(进来的朋友请留言,共同探讨

php中文网

php中文网

发布时间:2016-06-07 15:00:49

|

1311人浏览过

|

来源于php中文网

原创

今天用selenium和casperjs2种对https://class.coursera.org/nlp/lecture网站的ppt、pdf、srt、mp4的下载地址进行数据抓取 1、pythonselenium #!/usr/bin/python# -*- coding: utf-8 -*-from selenium import webdriverfrom bs4 import BeautifulSoupimport t

今天用selenium和casperjs2种对https://class.coursera.org/nlp/lecture网站的ppt、pdf、srt、mp4的下载地址进行数据抓取

1、python+selenium

#!/usr/bin/python
# -*- coding: utf-8 -*-

from selenium import webdriver
from bs4 import BeautifulSoup

import time
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

def catchDate(s):
    """页面数据提取"""
    soup = BeautifulSoup(s)
    z = []
    
    m = soup.findAll("ul",class_="course-item-list-div-list")
    
    for obj in m:
        try:
            print obj.previous_sibling.find('h3').get_text()
            tmp = obj.findAll('li', class_="unviewed")
            for eachli in tmp:
                titleli = eachli.find('a').get_text()
                print '          '+titleli
                allaInEachDiv = eachli.find('div', class_="course-lecture-item-resource").findAll('a')
                for eacha in allaInEachDiv:
                    print '      '+eacha['href']
        except Exception, e:
            continue
        if(tmp != ""):
            z.append(tmp)
    return z

starttime = time.time()
driver = webdriver.PhantomJS(executable_path='C:\phantomjs-1.9.7-windows\phantomjs.exe')
driver.get("https://class.coursera.org/nlp/lecture")
html = driver.page_source
content = catchDate(html)
endtime = time.time()
print endtime - starttime
driver.quit

2、casperjs
var casper = require("casper").create({
	clientScripts: ["jquery-1.7.js"],  
    stepTimeout: 120 * 1000,  
    pageSettings: {  
        loadImages: false  
    },  
    verbose: true,  
    logLevel: "error"  
});  
var numberOfLinks = 0;

var fs = require('fs');
var filename = 'content.txt';
var fullContent = "";
var startTime = new Date(), endTime;
  
casper.start("https://class.coursera.org/nlp/lecture", function() {
    numberOfLinks = this.evaluate(function() {
        return __utils__.findAll('.course-item-list-div-list').length;
    });
    this.echo(numberOfLinks + " items found");
});
getStartTime = function(){
	this.echo(startTime);
	this.then(getcontent);
};
getcontent = function() {
    fullContent = this.evaluate(function() {
        var content = "";
        jQuery('.course-item-list-div-list').each(function() {
			var btitle = $(this).prev().find("h3").text();
			content += btitle + '\r\n';
			$(this).find("li").each(function(){
				var stitle = $(this).find("a").first().text();
				content += stitle + '\r';
				$(this).find("div a").each(function(){
					content += $(this).attr("href")+'\r';
				});
				content += '\r\n';
			});
			content += '\r\n\r\n';
        });
        return content;
    });
	this.then(writefile);
};

writefile = function() {
    this.echo('writing to ' + filename);
    fs.write(filename, fullContent, 'w');
	this.then(getEndTime);
};
getEndTime = function(){
	endTime = new Date();
}
casper.then(getStartTime);
casper.then(function exitSystem() {
	this.echo(new Date() - startTime);
    casper.exit();  
}); 

casper.run();

因为不熟练,感觉写的不太好,求大神对方法进行指导!!!


参考:

https://gist.github.com/imjared/5201405

RoomGPT
RoomGPT

使用AI为每个人创造梦想的房间

下载

http://casperjs.readthedocs.org/en/latest/modules/casper.html#evaluate

http://blog.csdn.net/u012577500/article/details/18185399

http://stackoverflow.com/questions/14894311/casperjs-windows-installation-how-is-it-done-the-correct-way-please

http://blog.csdn.net/sagomilk/article/details/20800543

相关专题

更多
虚拟号码教程汇总
虚拟号码教程汇总

本专题整合了虚拟号码接收验证码相关教程,阅读下面的文章了解更多详细操作。

29

2025.12.25

错误代码dns_probe_possible
错误代码dns_probe_possible

本专题整合了电脑无法打开网页显示错误代码dns_probe_possible解决方法,阅读专题下面的文章了解更多处理方案。

20

2025.12.25

网页undefined啥意思
网页undefined啥意思

本专题整合了undefined相关内容,阅读下面的文章了解更多详细内容。后续继续更新。

37

2025.12.25

word转换成ppt教程大全
word转换成ppt教程大全

本专题整合了word转换成ppt教程,阅读专题下面的文章了解更多详细操作。

6

2025.12.25

msvcp140.dll丢失相关教程
msvcp140.dll丢失相关教程

本专题整合了msvcp140.dll丢失相关解决方法,阅读专题下面的文章了解更多详细操作。

2

2025.12.25

笔记本电脑卡反应很慢处理方法汇总
笔记本电脑卡反应很慢处理方法汇总

本专题整合了笔记本电脑卡反应慢解决方法,阅读专题下面的文章了解更多详细内容。

6

2025.12.25

微信调黑色模式教程
微信调黑色模式教程

本专题整合了微信调黑色模式教程,阅读下面的文章了解更多详细内容。

5

2025.12.25

ps入门教程
ps入门教程

本专题整合了ps相关教程,阅读下面的文章了解更多详细内容。

4

2025.12.25

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

218

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 4万人学习

PostgreSQL 教程
PostgreSQL 教程

共48课时 | 6.1万人学习

Git 教程
Git 教程

共21课时 | 2.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号