gpt4 book ai didi

javascript - phantomjs:document.querySelectorAll() 不适用于动态页面

转载 作者:行者123 更新时间:2023-12-03 22:58:54 26 4
gpt4 key购买 nike

我只是想从 this amazon URL 获取交易项目:
当我在浏览器中打开此链接并在控制台中编写查询时,它可以工作:document.querySelectorAll('div[class*="DealItem-module__dealItem_"]') document.querySelectorAll() result
但是当我尝试通过这个 phantomjs 获取它时脚本,它似乎总是什么都不返回:

var page = require('webpage').create();

page.viewportSize = { height: 800, width: 1920 }; // BRODIE : CHROME

page.customHeaders = {
accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
// 'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
dnt: '1',
'sec-ch-ua':
'" Not A;Brand";v="99", "Chromium";v="90", "Microsoft Edge";v="90"',
'sec-ch-ua-mobile': '?0',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.66',
};

page.settings.javascriptEnabled = true;
page.settings.loadImages = false;
//Script is much faster with this field set to false
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;

page.onConsoleMessage = function (message) {
console.log('console.log() -- ', message);
}; // BUBBLE UP LOGS FROM BROWSER CONSOLE TO PHANTOM CONSOLE

page.onLoadStarted = function () {
loadInProgress = true;
console.log('page loading started');
};
page.onLoadFinished = function () {
loadInProgress = false;
console.log('page loading finished');
};

page.onError = function (msg, trace) {
console.log(msg);
trace.forEach(function (item) {
console.log(' ', item.file, ':', item.line);
});
};

// OPEN PAGE
console.log('page.open()');
page.open(
'https://www.amazon.com/gp/goldbox/ref=gbps_ftr_s-5_cd34_wht_26179410?gb_f_deals1=sortOrder:BY_SCORE,includedAccessTypes:GIVEAWAY_DEAL,enforcedCategories:2617941011&pf_rd_p=fd51d8cf-b5df-4144-8086-80096db8cd34&pf_rd_s=slot-5&pf_rd_t=701&pf_rd_i=gb_main&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=A89BX6V6RQRQ94NFA0DP&ie=UTF8',
function (status) {
if (status !== 'success')
console.log('U N A B L E T O O P E N P A G E . . .');
else console.log(' P A G E O P E N E D . . .');

var selector = 'div[class*="DealItem-module__dealItem_"]'

var findAll = setInterval(function () {
console.log('trying to fetch deals...');
var deals = page.evaluate(function (sel) {
return document.querySelectorAll(
'div[class*="DealItem-module__dealItem_"]'
);
}, selector);

if(deals.length) {
console.log('deals.length', deals.length);
clearInterval(findAll);
}
}, 1000);
}
);
另外,当我尝试使用 page.render() 截屏时,它会显示带有未加载/未完成 JS 的页面( 与我们在浏览器中键入该 URL 并搜索时不同: ):
Unfinished/Unloaded JS page
另外,我注意到当我在终端中运行这个脚本时,我得到了一些网页的 JS 错误:
phantom JS webpage errors
任何帮助将不胜感激

最佳答案

根据documentation on the evaluate method in PhantomJS

Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.


Closures, functions, DOM nodes, etc. will not work!


相反,您应该在评估中执行长度计算,然后返回简单的原始长度。

关于javascript - phantomjs:document.querySelectorAll() 不适用于动态页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67725157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com