点击后无法抓取下一个网页

时间:2019-04-11 14:12:36

标签: javascript web-scraping phantomjs puppeteer

我正在尝试通过phantomjs编写脚本,以抓取一个表格拆分成一个网站的不同页面(目前是两个页面,但将来可能会更多或更少)。

我设法产生了两个带有所需内容的html输出,但是所产生的输出始终是第一个表,而不是第二个表。我尝试过包括等待页面加载的超时,但是它似乎不起作用。我已经测试了Chrome控制台上的下一个按钮的点击,并且可以正常工作。 不知道还缺少什么...

// Step 1: Open web page
var page = require('webpage').create();
var fs = require('fs');
function onPageReady() {
page.open('https://adb.taleo.net/careersection/1/jobsearch.ftl#');
phantom.waitFor(function() {return !page.loading;});

// Step 2: Scrape first table
var htmlContent = page.evaluate(function() {
    return document.documentElement.outerHTML;});
fs.write('C://MY_PATH' + '/outputadb.html', 
htmlContent,'w')

// Step 3: Click on button and wait for it to show
page.evaluate(function() { $("a#next").click(); });
phantom.waitFor(function() {
    return page.evaluate(function() {return $(".result-list- 
button").is(":visible");});
});
var htmlContent2 = page.evaluate(function() {
    return document.documentElement.outerHTML;});
fs.write('C://MY_PATH' + 
 '/outputadb2.html', htmlContent2,'w')
//console.log('READY!');
 phantom.exit();
}

phantom.waitFor = function(callback) {
  do {
   // Clear the event queue while waiting.
   // This can be accomplished using page.sendEvent()
   this.page.sendEvent('mousemove');
  } while (!callback());
 }

 onPageReady();

按照我曾尝试使用puppeteer的建议。 但是,在下面的尝试中,我得到对象承诺作为输出而不是html源代码。有什么想法吗?

const puppeteer = require('puppeteer');
const fs = require('fs');

(async function main() {

try {
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36')
await page.goto('https://adb.taleo.net/careersection/2/jobsearch.ftl#', { 
waitUntil: "networkidle2" });
await page.waitFor(1 * 1000);

const htmlContent =  page.evaluate(() => {
return document.documentElement.innerHTML})
body.innerHTML, bodyHandle);
console.log(htmlContent);
fs.writeFileSync("out.html", htmlContent);

await browser.close();
} catch (e) {
    console.log('our error',e)
}

})();

1 个答案:

答案 0 :(得分:0)

关于伪造者代码:您需要await使用伪造者的所有操作,例如

const htmlContent = await page.evaluate()
相关问题