首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加载JS后获取页面的HTML内容

问加载JS后获取页面的HTML内容
EN

Stack Overflow用户
提问于 2020-01-06 18:36:31
回答 1查看 104关注 0票数 1

我在试着从Rust文档中得到搜索结果。我做了这个代码来做它:

代码语言:javascript
复制
let HTMLParser = require('node-html-parser');
let https = require('https');
const search = "foo";
let options = {
    host: "doc.rust-lang.org",
    path: "/std/index.html?search=" + search
};

let request = https.get(options, (res) => {
    if (res.statusCode != 200) return console.log(`An error occured : ${res.statusCode}. Retry later.`);
    res.setEncoding("utf8");

    let output = "";

    res.on("data", (chunk) => {
        output += chunk
    });

    res.on("end", () => {
        let root = HTMLParser.parse(output);
        console.log(root.querySelector(".search-results")); // print "null" because the search is not done when the request response come
    });

    request.end();
});

但是,当我运行这段代码时,我会得到index.html页面的HTML,就像我请求这个页面而没有?search="foo"一样。我发现,当我们搜索某些内容时,页面会随某些JS动态变化,然后基本内容被设置为隐藏,搜索div变得可见。因此,当我得到请求结果时,JS似乎没有加载,但我需要它来获得文档中的搜索结果。我不知道我怎么能做到。提前感谢您的回答!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-06 18:56:06

执行搜索时,锈菌文档页面似乎没有触及后端。我使用浏览器开发工具注意到了这一点。

它看起来像是加载了一个搜索索引,其中包含了现成的文档。您可以使用这个js搜索文档。逻辑是在main.js中编写的。

如果您正在寻找更多的信息,请告诉我,因为我还没有了解如何创建每个文档项上的链接生成。

编辑

构建url所需的所有逻辑都在main.js中。该方法如下。如果仔细查看aliases.js、main.js、storage.js和search-index.js文件,可以重用几乎所有这些文件来创建链接和所需的搜索输出。

代码语言:javascript
复制
function buildHrefAndPath(item) {
      var displayPath;
      var href;
      var type = itemTypes[item.ty];
      var name = item.name;
      if (type === 'mod') {
        displayPath = item.path + '::';
        href = rootPath + item.path.replace(/::/g, '/') + '/' + name + '/index.html'
      } else if (type === 'primitive' || type === 'keyword') {
        displayPath = '';
        href = rootPath + item.path.replace(/::/g, '/') + '/' + type + '.' + name + '.html'
      } else if (type === 'externcrate') {
        displayPath = '';
        href = rootPath + name + '/index.html'
      } else if (item.parent !== undefined) {
        var myparent = item.parent;
        var anchor = '#' + type + '.' + name;
        var parentType = itemTypes[myparent.ty];
        if (parentType === 'primitive') {
          displayPath = myparent.name + '::'
        } else {
          displayPath = item.path + '::' + myparent.name + '::'
        }
        href = rootPath + item.path.replace(/::/g, '/') + '/' + parentType + '.' + myparent.name + '.html' + anchor
      } else {
        displayPath = item.path + '::';
        href = rootPath + item.path.replace(/::/g, '/') + '/' + type + '.' + name + '.html'
      }
      return [displayPath,
      href]
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59617234

复制
相关文章

相似问题

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