実際、オンになっていpage.settings
ます。の前に行いopen
ます。
リンクしたページに対して使用する例を次に示します。
var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36';
page.open('http://www.oddsportal.com/baseball/usa/mlb/results/page/', function() {
window.setTimeout(function() {
var output = page.evaluate(function() {
return document.getElementById('tournamentTable')
.getElementsByClassName('deactivate')[0]
.getElementsByTagName('a')[0]
.textContent;
});
console.log(output);
}, 1000);
});
この例では、テーブルの最初の行にある一致名をスクレイピングします。(この正確な瞬間は「San Francisco Giants - Boston Red Sox
」です)
あなたのコメントについて、実際にはphantomjsの下でjqueryを使用できます!この例を確認してください:
var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36';
page.open('http://www.oddsportal.com/baseball/usa/mlb/results/page/', function() {
window.setTimeout(function() {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js", function() {
var output = page.evaluate(function () {
return jQuery('#tournamentTable .deactivate:first a:first').text();
});
console.log(output);
});
}, 1000);
});
ちなみに、window.setTimeout
この例で使用したの代わりに、 waitfor.jsを使用することをお勧めします。