在不断增加的 URL 中搜索文本
Search Increasing URL For Text
对于 stekhn,这里有正确的链接:var location = "http://www.roblox.com/Trade/inventoryhandler.ashx?filter=0&userid=" + i + "&page=1&itemsPerPage=14";
我正在尝试创建一个 Javascript 脚本,我可以在其中搜索用户清单,检测他们的清单中是否有我正在寻找的东西,并输出用户 ID(如果他们有(。
如果我输入 bluesteel,我需要一个 Javascript 脚本,它将搜索 http://snackyrite.com/site.ashx?userid=1 并检测它上面是否有文本"bluesteel"——如果是,我需要它来显示用户 ID,即 1。
您可能认为这很容易,我可以轻松找到脚本 - 嗯,有一个问题,我的目标不仅是让它搜索 userid=1,我需要它从 userid=1 搜索到 userid=45356
如果在 userid=5、userid=3054 和userid=12 上找到单词"bluesteel"(这些只是示例(,我需要它在运行脚本的同一页面上显示 5、3054 和 12(ID(。
这是我尝试过的脚本,但用户 id 不会增加(我不确定该怎么做(。
var location = http://snackyrite.com/site.ashx?userid=1;
if(location.indexOf("bluesteel") > -1) {
output.userid
}
我很抱歉,Javascript不是我最好的。
使用循环:
for (var i = 1; i <=45356; i++) {
var loc = "http://snackyrite.com/site.ashx?userid="+i;
// get contents of location
if (contents.indexOf("bluesteel") > -1) {
console.log(i);
}
}
由于获取内容可能会使用 AJAX,因此if
可能会在回调函数中。看到Javascript臭名昭著的循环问题吗?了解如何编写循环,以便i
保留在回调函数中。
这种网页抓取无法在浏览器(客户端JavaScript(中完成。
我建议使用 Node.js 构建一个刮板。
- 安装节点.js
- 安装请求
npm i request
- 安装 cheerio
npm i cheerio
- 创建文件
scraper.js
- 运行
node scraper.js
scraper.js
代码
// Import the scraping libraries
var request = require("request");
var cheerio = require("cheerio");
// Array for the user IDs which match the query
var matches = [];
// Do this for all possible users
for (var i = 1; i <= 45356; i++) {
var location = "http://snackyrite.com/site.ashx?userid="+i;
request(location, function (error, response, body) {
if (!error) {
// Load the website content
var $ = cheerio.load(body);
var bodyText = $("body").text();
// Search the website content for bluesteel
if (bodyText.indexOf("bluesteel") > -1) {
console.log("Found bluesteel in inventory of user ", i);
// Save the user ID, if bluesteel was found
matches.push(i);
}
// Something goes wrong
} else {
console.log(error.message);
}
});
console.log("All users with bluesteel in inventory: ", matches);
}
上面的代码似乎有点复杂,但我认为这是应该完成的方式。当然,您可以使用任何其他抓取工具,库。
- 搜索文本文件并附加到表中
- 在网站HTML页面上搜索文本
- 在JavaScript中搜索文本并获取其所有开始和结束索引
- 不显示 jsTree 搜索文本框
- “搜索”文本仅考虑表格中的最后一列
- 突出显示搜索文本引导程序表
- jQuery自动完成下拉列表中的粗体搜索文本字符
- 在文档中搜索文本以删除的文本超过找到它的标签
- JavaScript:获取搜索文本在正文上的滚动位置
- 使用JavaScript搜索文本
- jQuery在多个表中搜索文本,如果找到文本,则单击按钮
- 使用javascript从html中搜索文本
- 使用PowerShell在JavaScript网页上搜索文本字符串
- Javascript Regex exec 在向搜索文本添加属性/引号后冻结
- 具有搜索文本的功能,不能很好地处理标点符号/符号.Jquery/Jquery mobile.
- Javascript打开带有搜索文本的新窗口
- 谷歌语音识别搜索文本问题
- 我们可以使数据列表自动完成以搜索文本和值吗?
- 使搜索文本框输入值不区分大小写
- 在嵌套对象(例如 Backbone.js集合)中搜索文本