Today, we use selenium and casperjs2 to capture the ppt, pdf, srt, and MP4 data of the class. coursera. orgnlplecture website. 1. pythonselenium #! Usrbinpython #-*-coding: UTF-8-*-fromseleniumimportwebdriverfrombs4importBeautifulSoupimportt
Today, we use selenium and casperjs2 to capture the incoming data of https://class.coursera.org/nlp/lecture.ppt?##srt=mp41. pythonselenium #! /Usr/bin/python #-*-coding: UTF-8-*-from selenium import webdriverfrom bs4 import BeautifulSoupimport t
Today, we use selenium and casperjs2 to capture the inbound data of the https://class.coursera.org/nlp/lecturewebsite ppt??#srt=mp4
1. python + selenium
#! /Usr/bin/python #-*-coding: UTF-8-*-from selenium import webdriverfrom bs4 import BeautifulSoupimport timeimport sysreload (sys) sys. setdefaultencoding ('utf-8') def catchDate (s): "page data extraction" soup = BeautifulSoup (s) z = [] m = soup. findAll ("ul", class _ = "course-item-list-p-list") for obj in m: try: print obj. previus_sibling.find ('h3 '). get_text () tmp = obj. findAll ('lil', class _ = "unviewed") for eachli I N tmp: titleli = eachli. find ('A '). get_text () print ''+ titleli allaInEachDiv = eachli. find ('P', class _ = "course-lecture-item-resource "). findAll ('A') for eacha in allaInEachDiv: print ''+ eacha ['href '] except t Exception, e: continue if (tmp! = ""): Z. append (tmp) return zstarttime = 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_sourcecontent = catchDate (html) endtime = time. time () print endtime-starttimedriver. 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-p-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-p-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("p 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();
Because I am not proficient in writing, I don't feel very good. Please give me guidance on the method !!!
Refer:
Https://gist.github.com/imjared/5201405
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