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

问题描述 投票:1回答:1

我正在尝试通过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();

根据我的建议,我试图使用木偶戏。然而,在我的尝试下面,我将对象承诺作为输出而不是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)
}

})();
javascript web-scraping phantomjs puppeteer
1个回答
0
投票

关于你的木偶操作员代码:你需要await所有与puppeteer一起工作的操作,例如

const htmlContent = await page.evaluate()
© www.soinside.com 2019 - 2024. All rights reserved.