Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
REMitchell
GitHub Repository: REMitchell/python-crawling
Path: blob/master/6-Selenium/phantomjs/examples/tweets.js
164 views
1
// Get twitter status for given account (or for the default one, "PhantomJS")
2
3
var page = require('webpage').create(),
4
system = require('system'),
5
twitterId = "PhantomJS"; //< default value
6
7
// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
8
page.onConsoleMessage = function(msg) {
9
console.log(msg);
10
};
11
12
// Print usage message, if no twitter ID is passed
13
if (system.args.length < 2) {
14
console.log("Usage: tweets.js [twitter ID]");
15
} else {
16
twitterId = system.args[1];
17
}
18
19
// Heading
20
console.log("*** Latest tweets from @" + twitterId + " ***\n");
21
22
// Open Twitter Mobile and, onPageLoad, do...
23
page.open(encodeURI("http://mobile.twitter.com/" + twitterId), function (status) {
24
// Check for page load success
25
if (status !== "success") {
26
console.log("Unable to access network");
27
} else {
28
// Execute some DOM inspection within the page context
29
page.evaluate(function() {
30
var list = document.querySelectorAll('div.tweet-text');
31
for (var i = 0; i < list.length; ++i) {
32
console.log((i + 1) + ": " + list[i].innerText);
33
}
34
});
35
}
36
phantom.exit();
37
});
38
39