我在试着从Rust文档中得到搜索结果。我做了这个代码来做它:
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似乎没有加载,但我需要它来获得文档中的搜索结果。我不知道我怎么能做到。提前感谢您的回答!
发布于 2020-01-06 18:56:06
执行搜索时,锈菌文档页面似乎没有触及后端。我使用浏览器开发工具注意到了这一点。
它看起来像是加载了一个搜索索引,其中包含了现成的文档。您可以使用这个js搜索文档。逻辑是在main.js中编写的。
如果您正在寻找更多的信息,请告诉我,因为我还没有了解如何创建每个文档项上的链接生成。
编辑
构建url所需的所有逻辑都在main.js中。该方法如下。如果仔细查看aliases.js、main.js、storage.js和search-index.js文件,可以重用几乎所有这些文件来创建链接和所需的搜索输出。
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]
}https://stackoverflow.com/questions/59617234
复制相似问题