首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何与木偶手从网站中获取所有链接

问如何与木偶手从网站中获取所有链接
EN

Stack Overflow用户
提问于 2021-06-01 00:22:02
回答 3查看 2.1K关注 0票数 6

好吧,我想要一种方法使用傀儡手和for循环来获取站点上的所有链接并将它们添加到数组中,在这种情况下,我想要的链接不是html标记中的链接,它们是直接在源代码、javascript文件链接等中的链接。我想要这样的东西:

代码语言:javascript
复制
array = [ ]
 for(L in links){
  array.push(L)
   //The code should take all the links and add these links to the array
 }

但是,我如何获得对javascript样式文件的所有引用,以及网站源代码中的所有URL?我只是找到一个帖子和一个问题,教或展示它如何从标签中获取链接,而不是从源代码中获取所有链接。

假设您希望获取此页面上的所有标记,例如:

视图-来源:https://www.nike.com/

如何获得所有脚本标记并返回控制台?我放view-source:https://nike.com是因为你可以得到脚本标签,我不知道你能不能在不显示源代码的情况下完成它,但是我想要显示和得到脚本标签,因为这就是我的想法,但是我不知道怎么做

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-06-05 03:27:07

只使用node.js就可以从URL中获取所有链接,而不需要傀儡:

有两个主要步骤:

  1. 获取URL的源代码。
  2. 解析链接的源代码。

在node.js:中的简单实现

代码语言:javascript
复制
// 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 (https://stackoverflow.com/a/3809435/117030):
    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;
}

示例用法:

代码语言:javascript
复制
$ 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
]

备注:

源代码,例如:

  • 本机node.js http/https
票数 6
EN

Stack Overflow用户

发布于 2021-10-06 11:22:42

虽然其他答案适用于许多情况,但它们不适用于客户端呈现的站点。例如,如果您只是对Reddit执行一个Axios请求,那么您将得到一些元数据的两个div。当Puppeteer实际获取页面并在真正的浏览器中解析所有JavaScript时,网站对文档呈现的选择与页面数据的提取无关。

在页面对象上有一个计算方法,允许您直接在页面上运行JavaScript。使用它,您可以轻松地提取所有链接如下:

代码语言:javascript
复制
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  
  const pageUrls = await page.evaluate(() => {
    const urlArray = Array.from(document.links).map((link) => link.href);
    const uniqueUrlArray = [...new Set(urlArray)];
    return uniqueUrlArray;
  });

  console.log(pageUrls);
 
  await browser.close();
})();
票数 2
EN

Stack Overflow用户

发布于 2021-06-05 23:54:03

是的,你可以得到所有的脚本标签和它们的链接,而无需打开视图源代码。您需要在项目中添加jsdom库的依赖项,然后向其实例传递HTML,如下所示

以下是代码:

代码语言:javascript
复制
const axios = require('axios');
const jsdom = require("jsdom");

// hit simple HTTP request using axios or node-fetch as you wish
const nikePageResponse = await axios.get('https://www.nike.com');

// now parse this response into a HTML document using jsdom library
const dom = new jsdom.JSDOM(nikePageResponse.data);
const nikePage = dom.window.document

// now get all the script tags by querying this page
let scriptLinks = []
nikePage.querySelectorAll('script[src]').forEach( script => scriptLinks.push(script.src.trim()));
console.debug('%o', scriptLinks)

在这里,我为<script>标记做了CSS选择器,其中包含了src属性。

您可以在使用傀儡机中编写相同的代码,但是打开浏览器和所有东西需要时间,然后得到它的pageSource。

你可以用这个找到链接,然后做任何你想用他们使用木偶或任何东西。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67781393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档