![](/img/trans.png)
[英]Azure and Node.js and JSDOM scheduled task - opening a webpage with scripts
[英]node.js request a webpage with async scripts
我正在使用請求模塊下載網頁,這非常簡單。
我的問題是我正在嘗試下載的頁面有一些異步腳本(具有異步屬性),並且它們不會從http請求中返回html文檔。
我的問題是我如何使用/ with-out(最好帶有)請求模塊發出http請求,並且由於某些邊緣情況,如上所述具有WHOLE頁面下載而沒有例外。
僅使用請求,您可以嘗試以下方法來提取異步腳本。
注意:我已經通過一個非常基本的設置對此進行了測試,並且還有一些工作要做,以使其健壯。 但是,它對我有用:
為了設置測試,我創建了一個html文件,其中包含一個主體中的腳本,如下所示: <script src="abc.js" async></script>
然后創建臨時服務器以啟動它(httpster)
"use strict";
const request = require('request');
const options1 = { url: 'http://localhost:3333/' }
// hard coded script name for test purposes
const options2 = { url: 'http://localhost:3333/abc.js' }
let htmlData // store html page here
request.get(options1)
.on('response', resp => resp.on('data', d => htmlData += d))
.on('end', () => {
let scripts; // store scripts here
// htmlData contains webpage
// Use xml parser to find all script tags with async tags
// and their base urls
// NOT DONE FOR THIS EXAMPLE
request.get(options2)
.on('response', resp => resp.on('data', d => scripts += d))
.on('end', () => {
let allData = htmlData.toString() + scripts.toString();
console.log(allData);
})
.on('error', err => console.log(err))
})
.on('error', err => console.log(err))
這個基本的例子有效。 您需要在頁面上找到所有js腳本並提取我在這里沒有完成的url部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.