Я пытаюсь запустить скрипт, чтобы получить ссылки на конкретный запрос c в bing.com, используя casper. js, но его ошибка генерации - PullRequest
0 голосов
/ 01 августа 2020

Я пытаюсь запустить скрипт для получения ссылок на определенные c запросы на bing.com с помощью casper. js, но его генерация ошибка. Это мой код

var casper = require('casper').create({
  verbose: true,
  logLevel: 'debug',
  clientScripts: ["vendor/jquery.min.js", "vendor/lodash.js"]
});

var links = [];

// function getLinks() {
//   // var links = document.querySelectorAll('.b_algo a');
//   var links = $('.b_algo a')
//   return Array.prototype.map.call(links, function(e) {
//     return e.getAttribute('href');
//   });
// }

function getLinks() {
  // var links = document.querySelectorAll('.b_algo a');
  var links = $('.b_algo a')
  return _.map(links, function(e) {
    return e.getAttribute('href');
  });
}

casper.start('http://bing.com/', function() {
  // search for 'casperjs' from google form
  this.fill('form[action="/search"]', {
    q: 'casperjs'
  }, true);
});

casper.then(function() {
  // aggregate results for the 'casperjs' search
  links = this.evaluate(getLinks);
  // now search for 'phantomjs' by filling the form again
  this.fill('form[action="/search"]', {
    q: 'phantomjs'
  }, true);
});

casper.then(function() {
  // aggregate results for the 'phantomjs' search
  links = links.concat(this.evaluate(getLinks));
});

casper.run(function() {
// echo results in some readable fashion
  this.echo(links.length + ' links found:');
  this.echo(' - ' + links.join('\n - ')).exit();
});

После запуска скриптов он генерирует этот журнал в CMD с некоторой ошибкой ... в основном, как этот: TypeError: null не является объектом (оценка 'links.concat')

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

F:\WEBSCRAPPING\CasperScripts>casperjs bing.js

F:\WEBSCRAPPING\CasperScripts>[info] [phantom] Starting...
[info] [phantom] Running suite: 4 steps
[debug] [phantom] opening url: http://bing.com/, HTTP GET
[debug] [phantom] Navigation requested: url=http://bing.com/, type=Other, willNa
vigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=http://www.bing.com/, type=Other, wi
llNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://www.bing.com/"
[warning] [phantom] Failed injecting vendor/jquery.min.js client side
?  Failed injecting vendor/jquery.min.js client side
[warning] [phantom] Failed injecting vendor/lodash.js client side
?  Failed injecting vendor/lodash.js client side
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/4 http://www.bing.com/ (HTTP 200)
[info] [remote] attempting to fetch form element from selector: 'form[action="/s
earch"]'
[debug] [remote] Set "q" field value to casperjs
[info] [remote] submitting form to /search, HTTP GET
[info] [phantom] Step anonymous 2/4: done in 7581ms.
[info] [phantom] Step anonymous 3/4 http://www.bing.com/ (HTTP 200)
[info] [remote] attempting to fetch form element from selector: 'form[action="/s
earch"]'
[debug] [remote] Set "q" field value to phantomjs
[info] [remote] submitting form to /search, HTTP GET
[info] [phantom] Step anonymous 3/4: done in 7626ms.
[info] [phantom] Step anonymous 4/4 http://www.bing.com/ (HTTP 200)
TypeError: null is not an object (evaluating 'links.concat')
  F:/WEBSCRAPPING/CasperScripts/phantomjs:/code/bing.js:43
  F:/WEBSCRAPPING/CasperScripts/phantomjs:/platform/casper.js:1685 in runStep
  F:/WEBSCRAPPING/CasperScripts/phantomjs:/platform/casper.js:414 in checkStep
[debug] [phantom] Navigation requested: url=http://www.bing.com/search?q=phantom
js&form=QBLH, type=FormSubmitted, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigat
e=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"

Заранее спасибо ...

...