A
A
Artem2015-07-30 11:24:48
JavaScript
Artem, 2015-07-30 11:24:48

How to increase the value of a variable in a loop?

Good afternoon!
I am doing a grabber of information from the site page.
I have this code:

var request = require('request');
var cheerio = require('cheerio');
var searchTerm = 'screen+scraping';
var page_counter = 1;
var jQuery = require('jQuery');
var url = 'http://www.domofond.ru/prodazha-kvartiry-belgorod-c310?Page="'+ page_counter+1 +'"&SortOrder=Newest';

for(page_counter = 1; page_counter < 5; page_counter++) {

request(url, function(err, resp, body){
$ = cheerio.load(body);


price = $('p.pull-left.df_listingTilePrice'); //цена
opisanie_k = $('p.df_listingTileType'); //краткое
place = $('p.df_listingTileAddress'); //адрес
opisanie_full = $('div.df_listingTileDescription')//полное описание


$(price).each(function(i,price){
    console.log($(price).text() + '\n');
});
$(opisanie_k).each(function(i,opisanie_k){
    console.log($(opisanie_k).text() + '\n');
});
$(place).each(function(i,place){
    console.log($(place).text() + '\n');
});
$(opisanie_full).each(function(i,opisanie_full){
    console.log($(opisanie_full).text() + '\n');
});


});

}

The bottom line is that there is a cycle that should be repeated 5 times, and display information from 5 different pages of the site. The script is currently not working correctly. I have 5 cycles, but information is displayed from the first page, and from the other (4 pages), information is not received. I understand that I need to increase the value in a variable, and then work with it, but I don’t know how to solve this problem correctly. I will be grateful for your help!
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Inchin ☢, 2015-07-30
@a420s

The url value must be set in a loop.

var page_counter = 2, url;
while(page_counter < 6){
    url = 'http://www.domofond.ru/prodazha-kvartiry-belgorod-c310?Page="'+(page_counter++)+'"&SortOrder=Newest';
    //Действие с url....
}

You can do the same with c for:
for(var page_counter = 2, url; page_counter < 6; page_counter++){
    url = 'http://www.domofond.ru/prodazha-kvartiry-belgorod-c310?Page="'+page_counter+'"&SortOrder=Newest';
    //Действие с url....
}

A
Artem, 2015-07-30
@a420s

The solution from Vitaliy Inchin is the right one! Thank you! The only thing didn't work because of one little thing - remove " " from url and leave only ' ' .
Everything is fine! Thank you !

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question