mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
Add sample scraping/automation example via Phantomjs
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
parent
12c12b1a47
commit
32ac150832
9
sample-functions/Phantomjs/Dockerfile
Normal file
9
sample-functions/Phantomjs/Dockerfile
Normal file
@ -0,0 +1,9 @@
|
||||
FROM alexellis2/phantomjs-docker:latest
|
||||
|
||||
ADD https://github.com/alexellis/faas/releases/download/0.5.6-alpha/fwatchdog /usr/bin
|
||||
RUN chmod +x /usr/bin/fwatchdog
|
||||
|
||||
ENV fprocess="phantomjs /dev/stdin"
|
||||
|
||||
HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1
|
||||
CMD ["fwatchdog"]
|
21
sample-functions/Phantomjs/README.md
Normal file
21
sample-functions/Phantomjs/README.md
Normal file
@ -0,0 +1,21 @@
|
||||
### Phantomjs function
|
||||
|
||||
[Phantomjs](http://phantomjs.org) is a headless web-browser used for scraping and automation testing. This function will scrape a web-page with the JavaScript sent in through the function.
|
||||
|
||||
Example usage:
|
||||
|
||||
```
|
||||
$ time curl --data-binary @cnn.js http://localhost:8080/function/phantomjs
|
||||
|
||||
Status: success
|
||||
CNN - Breaking News, Latest News and Videos
|
||||
|
||||
real 0m8.729s
|
||||
```
|
||||
|
||||
This script visits the front page of CNN and once it is fully loaded - scrapes the title.
|
||||
|
||||
See [cnn.js](https://github.com/alexellis/faas/tree/master/sample-functions/Phantomjs/cnn.js) as an example of a Phantomjs script.
|
||||
|
||||
Another example script [feedly_subscribers.js](https://github.com/alexellis/faas/tree/master/sample-functions/Phantomjs/feedly_subscribers.js) gives the count of subscribers for an RSS Feed registered on Feedly.
|
||||
|
10
sample-functions/Phantomjs/cnn.js
Normal file
10
sample-functions/Phantomjs/cnn.js
Normal file
@ -0,0 +1,10 @@
|
||||
var webPage = require('webpage');
|
||||
var page = webPage.create();
|
||||
|
||||
page.open('https://www.cnn.com/', function(status) {
|
||||
console.log('Status: ' + status);
|
||||
console.log(page.title);
|
||||
// Do other things here...
|
||||
phantom.exit();
|
||||
|
||||
});
|
13
sample-functions/Phantomjs/feedly_subscribers.js
Normal file
13
sample-functions/Phantomjs/feedly_subscribers.js
Normal file
@ -0,0 +1,13 @@
|
||||
var webPage = require('webpage');
|
||||
var page = webPage.create();
|
||||
|
||||
var feed = "http://blog.alexellis.io/rss/";
|
||||
var url = "https://feedly.com/i/subscription/feed/" + feed;
|
||||
|
||||
page.open(url, function(status) {
|
||||
var title = page.evaluate(function(s) {
|
||||
return document.querySelector(s).innerText;
|
||||
}, ".count-followers");
|
||||
console.log(title.split(" ")[0]);
|
||||
phantom.exit();
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user