当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Search Increasing URL For Text

问题描述:

For stekhn, here's the proper link: var location = "http://www.roblox.com/Trade/inventoryhandler.ashx?filter=0&userid=" + i + "&page=1&itemsPerPage=14";

I'm trying to create a Javascript script where I can search through a users inventory, detect if they have what I'm looking for in their inventory and output the userID if they have it.

If I type in bluesteel, I need a Javascript script which will search through http://snackyrite.com/site.ashx?userid=1 and detect if it has the text 'bluesteel' is on it - if it is, I need it to display the user id, which is 1.

You may be thinking that's easy and I can easily find the script for that - well there's a catch, my objective isn't only to get it to search userid=1, I need it to search from userid=1 up to userid=45356

If the word 'bluesteel' is found on userid=5, userid=3054 and userid=12 (these are just examples), I need it to display 5, 3054 and 12 (the ID's) on the same page where the script was ran from.

This is the script I've tried, but the userid won't increase (I'm not sure how to do that).

var location = http://snackyrite.com/site.ashx?userid=1;

if(location.indexOf("bluesteel") > -1) {

output.userid

}

I do apologize, Javascript isn't my best.

网友答案:

Use a loop:

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);
    }
}

Since getting the contents will presumably use AJAX, the if will probably be in the callback function. See Javascript infamous Loop issue? for how to write the loop so that i will be preserved in the callback function.

网友答案:

This kind of web scraping can't be done in the Browser (client-side JavaScript).

I would suggest building a scraper with Node.js.

  1. Install Node.js
  2. Install request npm i request
  3. Install cheerio npm i cheerio
  4. Create a file scraper.js
  5. Run node scraper.js

Code for 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);
}

The above code seems kind of complicated, but I think this is the way it should be done. Of corse you can use any other scraping tool, library.

分享给朋友:
您可能感兴趣的文章:
随机阅读: