2016-09-14 1 views
0

Loop Link Не позволяет соскабливать со своего сайта/базы данных другие базы данных. Это было просто для академических целей для безгласного тестирования браузера на динамических страницах загрузки. Пожалуйста, не злоупотребляйте их данными.Casper scrape multipages, которые загружаются на одной странице на основе javascript

В основном, я просто пытаюсь использовать ссылки на изображения с нескольких страниц, однако страницы загружаются динамически через java-скрипт. Так что я закончил тем, что пробовал безголовый браузер casperjs построен на phantomjs.

Теперь моя проблема заключается в том, что функция getThumbNails() выводит одни и те же данные дважды. Вот журнал выхода, обратите внимание, что первые '1' и первые '2' являются той же ссылкой.

[email protected]:~/WeB$ casperjs --web-security=no --cookies-file=/tmp/mycookies.txt 9Cas.js 
First Page Is Loaded 
Second Page Is Loaded 
1 http://x.lnimg.com/photo/thumb_480/40f175f108f5492b9cdec6486d753f8d.jpg 
1 http://x.lnimg.com/photo/thumb_480/29081ed96a6349a08c27424ce3bd2842.jpg 
1 http://x.lnimg.com/photo/thumb_480/29cd278e7cc34d9782d0a22782af2134.jpg 
1 http://x.lnimg.com/photo/thumb_480/3979dc0f0987407bb9f825f2a0cb3fa9.jpg 
1 http://x.lnimg.com/photo/thumb_480/dd06239abbf1433099ad3278607e5d7f.jpg 
1 http://x.lnimg.com/photo/thumb_480/6e7c6b6076d5414b8ee59baed3dc3131.jpg 
1 http://x.lnimg.com/photo/thumb_480/97027946bbf745a59d44ac1c3e9d22fe.jpg 
1 http://x.lnimg.com/photo/thumb_480/396fd224e85f42aea7a10e1873ed627c.jpg 
1 http://x.lnimg.com/photo/thumb_480/62f6afc3f50942388df9fe66e99a2ab4.jpg 
1 http://x.lnimg.com/photo/thumb_480/09ba5b97da4e47b1a97bac86e125001d.jpg 
1 http://x.lnimg.com/photo/thumb_480/580dcc66cd7f48d8aae8f583cd8e5e4b.jpg 
1 http://x.lnimg.com/photo/thumb_480/e62f6d11449b41ff93e191f6045cb304.jpg 
1 http://x.lnimg.com/photo/thumb_480/596230f07ebf471383991a99bd43420a.jpg 
1 http://x.lnimg.com/photo/thumb_480/da63be695fed4617b594d19e4aa0dc7a.jpg 
1 http://x.lnimg.com/photo/thumb_480/58f28244a2494c868006a97534e694e6.jpg 
1 http://x.lnimg.com/photo/thumb_480/1b0246dfe4314404b32147061198cf80.jpg 
1 http://x.lnimg.com/photo/thumb_480/b10a9bb8252f4d3d9638284f6f68fd6b.jpg 
1 http://x.lnimg.com/photo/thumb_480/48bf185da5f74a369dd629dd183bc8b3.jpg 
1 http://x.lnimg.com/photo/thumb_480/7008392330f847ecb3af7058ce0b2e3b.jpg 
1 http://x.lnimg.com/photo/thumb_480/fcf5618fc7d04265a912cce9f7dec344.jpg 
1 //x.lnimg.com/images/search/map/pinprofileclose2.gif 
1 
1 //x.lnimg.com/images/search/map/pinprofile-ViewDetails.gif 
1 http://ak.t0.tiles.virtualearth.net/tiles/r0?g=5289&mkt=en-us&lbl=l0&stl=h&shading=hill&n=z 
1 http://ak.dynamic.t1.tiles.virtualearth.net/comp/ch/0231?mkt=en-us&it=G,L&shading=hill&og=132&n=z&key=AhfWRPlwZlVGewL9th5vsIklmzvMkUMm8wx6pz5nXzgDlYXliZFSr6lBvgHYCZQj 
2 http://x.lnimg.com/photo/thumb_480/40f175f108f5492b9cdec6486d753f8d.jpg 
2 http://x.lnimg.com/photo/thumb_480/29081ed96a6349a08c27424ce3bd2842.jpg 
2 http://x.lnimg.com/photo/thumb_480/29cd278e7cc34d9782d0a22782af2134.jpg 
2 http://x.lnimg.com/photo/thumb_480/3979dc0f0987407bb9f825f2a0cb3fa9.jpg 
2 http://x.lnimg.com/photo/thumb_480/dd06239abbf1433099ad3278607e5d7f.jpg 
2 http://x.lnimg.com/photo/thumb_480/6e7c6b6076d5414b8ee59baed3dc3131.jpg 
2 http://x.lnimg.com/photo/thumb_480/97027946bbf745a59d44ac1c3e9d22fe.jpg 
2 http://x.lnimg.com/photo/thumb_480/396fd224e85f42aea7a10e1873ed627c.jpg 
2 http://x.lnimg.com/photo/thumb_480/62f6afc3f50942388df9fe66e99a2ab4.jpg 
2 http://x.lnimg.com/photo/thumb_480/09ba5b97da4e47b1a97bac86e125001d.jpg 
2 http://x.lnimg.com/photo/thumb_480/580dcc66cd7f48d8aae8f583cd8e5e4b.jpg 
2 http://x.lnimg.com/photo/thumb_480/e62f6d11449b41ff93e191f6045cb304.jpg 
2 http://x.lnimg.com/photo/thumb_480/596230f07ebf471383991a99bd43420a.jpg 
2 http://x.lnimg.com/photo/thumb_480/da63be695fed4617b594d19e4aa0dc7a.jpg 
2 http://x.lnimg.com/photo/thumb_480/58f28244a2494c868006a97534e694e6.jpg 
2 http://x.lnimg.com/photo/thumb_480/1b0246dfe4314404b32147061198cf80.jpg 
2 http://x.lnimg.com/photo/thumb_480/b10a9bb8252f4d3d9638284f6f68fd6b.jpg 
2 http://x.lnimg.com/photo/thumb_480/48bf185da5f74a369dd629dd183bc8b3.jpg 
2 http://x.lnimg.com/photo/thumb_480/7008392330f847ecb3af7058ce0b2e3b.jpg 
2 http://x.lnimg.com/photo/thumb_480/fcf5618fc7d04265a912cce9f7dec344.jpg 
2 //x.lnimg.com/images/search/map/pinprofileclose2.gif 
2 
2 //x.lnimg.com/images/search/map/pinprofile-ViewDetails.gif 
2 http://ak.t0.tiles.virtualearth.net/tiles/r0?g=5289&mkt=en-us&lbl=l0&stl=h&shading=hill&n=z 
2 http://ak.dynamic.t1.tiles.virtualearth.net/comp/ch/0231?mkt=en-us&it=G,L&shading=hill&og=132&n=z&key=AhfWRPlwZlVGewL9th5vsIklmzvMkUMm8wx6pz5nXzgDlYXliZFSr6lBvgHYCZQj 

Вот как документация говорит, оценивать работы: Casperjs Evaluate Diagram

Вот сайт я выскабливание: http://looplink.ensemblere.com/SearchResults

Я бегу сценарий через Баш на Linux с помощью:

casperjs --web-security=no --cookies-file=/tmp/mycookies.txt Script.js 
var casper = require('casper').create({ 
    verbose: true, 
}); 

//two different vars for two different pages of images 
var thumbNails, thumbNails2; 

function getThumbNails() { 
// Function to Scrape the links of thumbnails 
    var thumbNails = document.querySelectorAll('img'); 
    return Array.prototype.map.call(thumbNails, function (e) { 
     return e.getAttribute('src'); 
    }); 
}; 

// Scrubs target page below 
casper.start('http://looplink.ensemblere.com/SearchResults'); 

//Runs the function on page one, below 
casper.then(function() { 
    thumbNails = this.evaluate(getThumbNails); 
}); 

//Confirm that we are on page one of website, below 
casper.then(function(){ 
    if (this.exists('a.searchPagingButton.pagingPreviousButton')) { 
     this.echo('Second Page Is Loaded'); 
    } 
    else { 
     this.echo('First Page Is Loaded'); 
    } 
}); 

//Click on the next button to go to page two, below 
casper.thenClick('a.searchPagingButton.pagingNextButton'); 

//Confirm that we are on page two, below 
casper.then(function(){ 
    if (this.exists('a.searchPagingButton.pagingPreviousButton')) { 
     this.echo('Second Page Is Loaded') 
    } 
    else { 
     this.echo('First Page Is Loaded') 
    } 
}); 

//Runs the funciton for the second time, below; BUT HERE IS THE ISSUE ***** 
//idk why it pulls the same data twice, when the then statement above confirms we are on page two. 
casper.then(function() { 
    thumbNails2 = this.evaluate(getThumbNails); 
}); 

//Sort the data from the varaiables. 
casper.run(function() { 
    for(var i in thumbNails) { 
     console.log('1 ' + thumbNails[i]); 
    } 
    for(var i in thumbNails2) { 
     console.log('2 ' + thumbNails2[i]); 
    } 
    this.done(); 
}); 
+0

Вы сделали снимок экрана, чтобы убедиться, что находитесь на второй странице? –

+0

Кажется, оба набора ссылок одинаковы, но являются ли они с первой или второй? – deltab

+0

@ArtjomB. У меня нет, но это хороший тест, попробуй. Класс «.pagingPreviousButton» находится только на второй странице, поэтому я был уверен в этом, но все равно попробую :) – Votlon

ответ

0

Хорошо, поэтому реквизит до Artjom B. в комментариях для того, чтобы сообщить мне, чтобы он скриншот, чтобы убедиться, что страница загрузилась!

ресурсы были просто погрузки, а именно: still loading picture

добавлением

this.wait(40000, function() { 
    this.echo('Waited for 40 seconds'); 
}); 

Casper будет ждать 40 секунд

Для скриншоте отладки просто попробуйте this.capture(/home/SuperDankDude/yourfilename.png)

Я также заметил, что мобильный версия этого сайта отличается от версии на весь экран, поэтому мне также пришлось использовать casper.options.viewportSize = {width: 1920, height: 1080};

var casper = require('casper').create({ 
    verbose: true, 
}); 

//two different vars for two different pages of images 
var thumbNails, thumbNails2; 

casper.options.viewportSize = {width: 1920, height: 1080}; 

function getThumbNails() { 
// Function to Scrape the links of thumbnails 
    var thumbNails = document.querySelectorAll('img'); 
    return Array.prototype.map.call(thumbNails, function (e) { 
     return e.getAttribute('src'); 
    }); 
}; 

// Scrubs target page below 
casper.start('http://looplink.ensemblere.com/SearchResults'); 

//Runs the function on page one, below 
casper.then(function() { 
    thumbNails = this.evaluate(getThumbNails); 
}); 

//Confirm that we are on page one of website, below 
casper.then(function(){ 
    if (this.exists('a.searchPagingButton.pagingPreviousButton')) { 
     this.echo('Second Page Is Loaded'); 
    } 
    else { 
     this.echo('First Page Is Loaded') 
     this.capture("/home/votlon/WeB/firstpage.png"); 
    } 
}); 

//Click on the next button to go to page two, below 
casper.thenClick('a.searchPagingButton.pagingNextButton'); 

//Confirm that we are on page two, below 
casper.then(function(){ 
    if (this.exists('a.searchPagingButton.pagingPreviousButton')) { 
     this.echo('Second Page Is Loading') 
     this.wait(40000, function() { 
     this.echo('Waited for 40 seconds'); 
    }); 
     this.capture("/home/votlon/WeB/secondpage.png"); 
    } 
    else { 
     this.echo('First Page Is Loaded') 
    } 
}); 

//Runs the funciton for the second time, below; BUT HERE IS THE ISSUE ***** 
//idk why it pulls the same data twice, when the then statement above confirms we are on page two. 
casper.then(function() { 
    thumbNails2 = this.evaluate(getThumbNails); 
}); 

//Sort the data from the varaiables. 
casper.run(function() { 
    for(var i in thumbNails) { 
     console.log('1 ' + thumbNails[i]); 
    } 
    for(var i in thumbNails2) { 
     console.log('2 ' + thumbNails2[i]); 
    } 
    this.done(); 
}); 
+0

Вам не нужно ждать 40 секунд. Это слишком долго. Существует модальный диалог, отображаемый в течение короткого промежутка времени между асинхронными загрузками страниц. Используйте первый 'casper.waitUntilVisible (theUpdateModal)', а затем 'casper.waitWhileVisible (theUpdateModal)' для соответствия этому обходу. –

Смежные вопросы