6 html javascript node.js puppeteer
好吧,我想要一种方法来使用 puppeteer 和 for 循环来获取站点上的所有链接并将它们添加到数组中,在这种情况下,我想要的链接不是 html 标签中的链接,它们是链接直接在源代码、javascript 文件链接等中......我想要这样的东西:
array = [ ]
for(L in links){
array.push(L)
//The code should take all the links and add these links to the array
}
Run Code Online (Sandbox Code Playgroud)
但是如何获取对网站源代码中的 javascript 样式文件和所有 URL 的所有引用?我只是找到一个帖子和一个问题,教或展示它如何从标签中获取链接,而不是从源代码中获取所有链接。
假设您想获取此页面上的所有标签,例如:
查看源:https : //www.nike.com/
如何获取所有脚本标签并返回控制台?我view-source:https://nike.com之所以这样说是因为您可以获得脚本标签,我不知道您是否可以在不显示源代码的情况下做到这一点,但是我考虑过显示和获取脚本标签,因为这是我的想法,但是我不知道如何去做吧
可以仅使用 node.js 从 URL 获取所有链接,而无需 puppeteer:
主要有两个步骤:
node.js 中的简单实现:
// get-links.js
///
/// Step 1: Request the URL's html source.
///
axios = require('axios');
promise = axios.get('https://www.nike.com');
// Extract html source from response, then process it:
promise.then(function(response) {
htmlSource = response.data
getLinksFromHtml(htmlSource);
});
///
/// Step 2: Find links in HTML source.
///
// This function inputs HTML (as a string) and output all the links within.
function getLinksFromHtml(htmlString) {
// Regular expression that matches syntax for a link (/sf/answers/266660481/):
LINK_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi;
// Use the regular expression from above to find all the links:
matches = htmlString.match(LINK_REGEX);
// Output to console:
console.log(matches);
// Alternatively, return the array of links for further processing:
return matches;
}
Run Code Online (Sandbox Code Playgroud)
示例用法:
$ node get-links.js
[
'http://www.w3.org/2000/svg',
...
'https://s3.nikecdn.com/unite/scripts/unite.min.js',
'https://www.nike.com/android-icon-192x192.png',
...
'https://connect.facebook.net/',
... 658 more items
]
Run Code Online (Sandbox Code Playgroud)
笔记:
| 归档时间: |
|
| 查看次数: |
288 次 |
| 最近记录: |