您好我正在尝试解析一个有足球比分直播的网站。一起使用NodeJS和PhantomJS(phantomjs-node)。问题是我无法在page.evaluate
中对元素运行JQuery,因为我假设页面内容没有我要解析的元素。这里我有一个简单的例子,我看不到的元素叫做“resultsList”。如果我在Web浏览器的控制台上运行$("#resultsList").html()
,我可以获得内部html。
var phantom = require('phantom');
async function getHtml() {
const instance = await phantom.create([
"--load-images=false"
]);
const page = await instance.createPage();
await page.on("onResourceRequested", function(requestData) {
console.info('Requesting', requestData.url)
});
const status = await page.open('http://www.mackolik.com/canli-sonuclar');
console.log("STATUS: " + status);
const content = await page.property('content');
//console.log(content);
const result = await page.evaluate(function() {
return $("#resultsList").html();
});
console.log("Result : " + result);
await instance.exit();
};
getHtml();
输出的最新行是
Requesting http://track.adform.net/Serving/TrackPoint/?pm=775868&ADFPageName=Master%20tag&ADFdivider=%7C&ord=192742255982&Set1=en-US%7Cen-US%7C1024x768%7C32&ADFtpmode=3&loc=http%3A%2F%2Fwww.mackolik.com%2FGoogleBanner.htm%3Fv%3D1.150&atdt=.Wa44j1e3NlY5BSo9z4ofjb75PaK4Vpjt3Q9cUVlOrXTAxw63UYOKES5jfyEwHXXTSHCSPxQEt05oaYAhrcpMxE6DBUrizAjVlpSnGmfDnxf7_OLjLnidPZW2AUMnGWVQdgMVQdgGgeVjrkRGjftckcKyAd65hz7YOK2w5ADwIlUjVsYwQ9dvcpxUlzXJJIneGffMfOz77qZoOSix5ezdstlYyxrtSw5DfiPDKOqUhO3f9puqSySy.aPjgnYVuxISg0QirQjEKZtlGqfstpBSKxUC56MnGWpwoNSUC550ial.rIN96h1P38Z_..hw6LzSIrwyMSu_djmVzgzH_y3EjNrjOxCRg9Vr_0p5DtQWug0Jl5mQeQzJQSuX3jtFTL4z1dyJjjNrjOxCUciL4FQmVurNW5BNlYiJ.lY5BqNAE.lTjV..G1
Requesting http://cdn.dimml.io/static/0025b42fc3958ae34c902ccc8164863b1d4b6019.js
STATUS: success
Requesting http://baltar.dimml.io/flow/oiod/0025b42fc3958ae34c902ccc8164863b1d4b6019.js?clientId=2&dom=www.mackolik.com&url=http%3A%2F%2Fwww.mackolik.com%2Fcanli-sonuclar&gemius_sent_once=1
Result : null
如您所见,“结果”打印为空。
我使用命令>node --harmony-async-await phantomTest.js
编辑:网页有JQuery库。所以,我不需要使用includeJs()
来添加JQuery。
Requesting http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
Requesting http://cm.cdn.md/js5/jquery-ui-1.8.23.custom/js/jquery-ui-1.8.23.custom.min.js
Requesting http://cm.cdn.md/js5/jquery-ui-1.8.20.custom/css/custom-theme/jquery-ui-1.8.20.custom.css
Requesting http://cm.cdn.md/js5/jquery.jsonp-2.1.4.js
Requesting http://cm.cdn.md/js5/jquery-ui-1.8.20.custom/development-bundle/ui/i18n/jquery.ui.datepicker-tr.js
Requesting http://cm.cdn.md/js5/jquery-ui-1.8.20.custom/development-bundle/themes/custom-theme/jquery.ui.datepicker.css
Requesting http://cm.cdn.md/js5/jquery-autocomplete/jquery.autocomplete.min.js
答案 0 :(得分:-1)
有两种可能性
网站里面没有jQuery 解决方案:injectJs(“/ 系统中jQuery的绝对路径 /”);