【发布时间】:2016-07-20 16:39:38
【问题描述】:
首先,让我指出我是 node.js 和异步编程的新手,所以我的代码可能真的很糟糕。我正在尝试使用 webdriverio 和 Cheerio 构建一个 webscraper。在这个网络爬虫中,我必须进行查询,在内容页面和结果页面之间移动时抓取查询的结果,然后在结果用完后执行新的查询。这是我到目前为止提出的代码(假设客户端已经启动,并且正在从“.then()”操作调用函数“make_new_query()”):
function scrapt_content(){
// if array of content links is exhausted -> move to next page or perform new query
if(contents_pointer == contents.length){
return client.isExisting("li.next-page > a").then(function(isExisting){
// if there is a link to a a new page of results -> move to new page
if(isExisting){
return change_pages();
} else {
return make_new_query();
};
});
// change to new and scrapt it
} else {
// var parsed = cheerio.load(res);
... scrap content using cherio ...
.
.
.
contents_pointer++;
return scrapt_content();
})
};
};
function change_pages(){
client
.click("li.next-page > a")
.getAttribute("h2 a", "href");
.then(function(res){
contents_pointer = 0;
news_links = res;
return scrapt_content();
})
}
function make_new_query(){
.
.
.
client.url(new_query_url)
.getAttribute("h2 > a", "href")
.then(function(res){
content_links = res;
return scrapt_content();
})
}
}
问题是,在到达要抓取的第一页内容后(代码执行查询并进入 content_links 数组中的第一个链接的页面),webdriver 关闭。就像代码首先执行函数change_pages,该函数调用了scrapt_content,然后提前终止。因此,我假设在此函数中链接操作时出现错误。在尝试链接这些操作时,谁能指出我的错误?
【问题讨论】:
标签: javascript node.js webdriver-io