进行循环以抓取 Google 下一页 Watin 时出错 [英] Error When making a loop to crawl Google next page Watin

查看:33
本文介绍了进行循环以抓取 Google 下一页 Watin 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的代码可以帮助我爬到 Google 结果页的第 2 页:

I have a simple code that help me crawl to page 2 of Google result page:

var ie= new IE();
ie.Link(Find.ByText("2")).Click(); 

我想要的只是用输入的页数爬到下一页,这样我就可以像这样循环:

All I want is crawling to more next page with the inputed number of page, so that I make a loop like this:

                string[] page = null;
                for (int i = 0; i < NumOfPage; i++)
                {
                 Array.Resize<string> (ref page, i+1);
                page[i] = "\"" + i.ToString() + "\"";
                }
               int count=2;
               while (count<NumOfPage)
                {
                     ie.Link(Find.ByText(page[count])).Click();                                                                                
                     count++;    
                }

但结果是在第一页暂停,没有爬到下一页.似乎循环不起作用.问题出在哪里???

But the result is it pause at the first page, no crawling to the next page. It's seem the loop doesn't work. Where is the problem???

推荐答案

       // Setup browser object
        var browser = new IE();
        var url = "www.google.com";
        browser.GoTo(url);

        var searchBox = browser.TextField(Find.ByName("q"));
        searchBox.Value="Rex";

        //click on the search button
        var btnSearch = browser.Button(Find.ByValue("Search"));
        btnSearch.Click();
        //wait for browser to load properly
        browser.WaitForComplete();

        // Find the navigation menu table            
        var navigationtable = browser.Table(Find.ById("nav"));

        // To go to the second page
        var secondpage = navigationtable.Link(Find.ByText("2"));
        secondpage.Click();
        //wait for browser to load properly
        browser.WaitForComplete();

这只是进入第二页现在如果你想循环

this simply goes to the second page now if you want to loop through

然后

for (int i = 2; i <= 10; i++)
{
var nextpage = navigationtable.Link(Find.ByText("i"));
//check if the link exists
/if yes then click on it
if(nextpage.Exists)
nextpage.click();
browser.waitforComplete
}

这篇关于进行循环以抓取 Google 下一页 Watin 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆