问题描述
我想找到一种方法来循环所有国家。
i am trying to figureout a way to loop all countries.
我在脚本下面有3个循环.Loop#2,3工作正常。但循环#1不是。
这里是逻辑 - 主要的alexa网站www.alexa.com/topsites/countries,这有多个国家,每个国家都有2个字符后缀。对于每个国家/地区编号,前缀为获取国家/地区列表
I have below script it has 3 loops.Loop # 2, 3 working fine. but Loop #1 is not.here is the logic - main alexa site www.alexa.com/topsites/countries and this has multiple countries each country has a 2 charecter suffix. for each country number is prefixing to get country listing.
流程:1。循环#1访问www.alexa.com/topsites/countries并循环所有国家。(此部分不起作用)2.loop#2对于每个国家/地区循环所有页面(此部分也有效)3。循环#3为每个页面收集数据。 (这部分工作)
process: 1. loop#1 go to www.alexa.com/topsites/countries and loop all countries.(this part does not work) 2.loop#2 for each country loop all pages(this part also working) 3. loop# 3 for each page collect data. (this part is working)
var jsLF="\n";
var macro;
macro = "CODE:";
macro += "VERSION BUILD=9002379" + jsLF;
macro += "TAB T=1" + jsLF;
macro += "TAB CLOSEALLOTHERS" + jsLF;
macro += "TAG POS={{i}} TYPE=A ATTR=HREF:* EXTRACT=TXT" + jsLF;
macro += "SAVEAS TYPE=EXTRACT FOLDER=C:\\ FILE=hiprsites.txt" + jsLF;
var macro1;
macro1 = "CODE:";
macro1 += "VERSION BUILD=9002379" + jsLF;
macro1 += "TAB T=1" + jsLF;
macro1 += "TAB CLOSEALLOTHERS" + jsLF;
macro1 += "URL GOTO=http://www.alexa.com/topsites/countries;{{j}}/ID" + jsLF;
var macroAllC;
macroAllC = "CODE:";
macroAllC += "VERSION BUILD=9002379" + jsLF;
macroAllC += "TAB T=1" + jsLF;
macroAllC += "TAB CLOSEALLOTHERS" + jsLF;
macroAllC += "URL GOTO=http://www.alexa.com/topsites/countries;{{z}}" + jsLF;
//LOOP #1
//loop all countries take one country and go to next loop
for (var z=0;z<200;z++)
{
iimDisplay(z);
iimSet("z", z);
iimPlay(macroAllC);
//LOOP #2
//loop all the pages for each page get data
for (var j=0;j<20;j++)
{
iimDisplay(j);
iimSet("j", j);
iimPlay(macro1);
//LOOP #3
//loop the current page and get all 25 result
for(var i=1;i<=25;i++)
{
iimDisplay(i);
iimSet("i", i);
iimPlay(macro);
iimSet("i",i);
}//loop individual pages
}//loop macro1
推荐答案
首先,您需要提取临时数组中的所有国家/地区:
First of all you need to extract all countries in a temporary array:
var countries = new Array(), i = 4;
do
{
iimDisplay("Extracting " + i);
iimSet("i", i);
iimPlay("CODE:TAG POS={{i}} TYPE=A ATTR=HREF:*countries* EXTRACT=HREF");
if(iimGetLastExtract()!='#EANF#')
countries.push(iimGetLastExtract());
else break;
i++;
}
while(i);
我设置 i = 4
到得到第一个国家 AF
I'm setting i=4
to get the first country AF
然后你必须遍历每个国家页面并提取另一个国家的所有数据数组:
Then you will have to loop through each country page and extract all data in another temporary array:
for(i=0;i<countries.length;i++)
{
var j = 2;
iimSet("url", countries[i]);
iimPlay("CODE:URL GOTO={{url}}");
do
{
iimDisplay("Loop " + (i+1) + " of " + countries.length + "\nExtracting " + j);
iimSet("j", j);
iimPlay("CODE:SET !TIMEOUT_STEP 1\nTAG POS={{j}} TYPE=A ATTR=HREF:*siteinfo* EXTRACT=HREF");
if(iimGetLastExtract()!='#EANF#')
temp_pages.push(iimGetLastExtract());
else
{
iimPlay("CODE:TAG POS=1 TYPE=A ATTR=TXT:Next");
if(iimGetLastError()=='OK')
j = 1;
else
break;
}
j++;
}
while(j);
}
现在我们将所有国家/地区的所有网页存储在 temp_pages
数组,你必须循环并提取你需要的数据:
Now we have all pages from all countries stored in temp_pages
array, which you will have to loop and extract the data you need:
for(i=0;i<temp_pages.length;i++)
{iimDisplay("Loop " + (i+1) + " of " + temp_pages.length);
iimSet("url", temp_pages[i]);
iimPlay("CODE:URL GOTO={{url}}");
j = 1;
do
{
iimSet("j", j);
iimPlay("CODE:TAG POS={{j}} TYPE=A ATTR=HREF:* EXTRACT=TXT");
if(iimGetLastExtract()!='#EANF#')
{
iimSet("ext", iimGetLastExtract());
iimPlay("CODE:SET !EXTRACT {{ext}}\nSAVEAS TYPE=EXTRACT FOLDER=C:\\ FILE=hiprsites.txt");
}
else break;
j++;
}
while(j);
}
因此您将拥有 hiprsites.txt
很多锚点(不知道为什么需要这些信息)。如果要提取 hrefs
,则更改 TAG POS = {{j}} TYPE = A ATTR = HREF:* EXTRACT = TXT
到 TAG POS = {{j}} TYPE = A ATTR = HREF:* EXTRACT = HREF
。
As a result you will have in hiprsites.txt
a lot of anchors (don't know why you need this information). If you want to extract hrefs
, then change TAG POS={{j}} TYPE=A ATTR=HREF:* EXTRACT=TXT
to TAG POS={{j}} TYPE=A ATTR=HREF:* EXTRACT=HREF
.
祝你好运)
这篇关于imacros javascript multiple for loops内循环过程提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!