简体   繁体   English

单击按钮添加dom后,如何运行我的js函数

[英]How to run my js function after clicked a button add the dom has reloaded

I want to use js to crawl a website, But the website use the ajax to paging the contents. 我想使用js来爬行网站,但是该网站使用ajax来分页内容。 At first, you can only crawl the first page content. 首先,您只能抓取首页内容。 the you must click a button(next page), the website use the ajax to update content of the webpage. 您必须单击一个按钮(下一页),网站使用ajax更新网页的内容。

Qustion: I can use js to find the button, and click it. 问题:我可以使用js查找按钮,然后单击它。 but how I know when the page has reloaded.and I can to get updated contents. 但是我怎么知道页面何时重新加载。我可以获取更新的内容。

Thanks. 谢谢。

I would use a "headless" browser for such a task: 我将使用“无头”浏览器执行以下任务:

PhantomJS 幻影

Casper 卡斯珀

Especially the click function of casperJS could be used to what you intended. 特别是casperJS的click函数可以用于您的预期。

Or the given exmple to fill out forms is dead simple: 或给定的示例来填写表格非常简单:

casper.start('http://google.fr/', function() {
    // search for 'casperjs' from google form
    this.fill('form[action="/search"]', { q: 'casperjs' }, true);
});

From Quickstart of casperJs. 从casperJs的快速入门开始。

In this case, where the web context that you are trying to crawl includes dynamic content through AJAX, CasperJS is an excellent option if you want to use Javascript in order to achieve that. 在这种情况下,如果您要爬网的Web上下文包含通过AJAX进行的动态内容,那么如果您想使用Javascript来实现此目的,CasperJS是一个很好的选择。 You can use it to trigger events, add process steps, include functions to wait and validate after each ajax call before to process any next step. 您可以使用它来触发事件,添加处理步骤,包括在每个ajax调用之后等待并验证然后再处理任何下一步的函数。

Here an example how crawl a website with CasperJS and JQuery 这是一个如何使用CasperJS和JQuery搜寻网站的示例
CasperJs and Jquery with chained Selects 带有选择链的CasperJs和Jquery

Here an example how crawl a website with CasperJS and just Javascript 这是一个如何使用CasperJS和Javascript抓取网站的示例
CasperJS dynamic selectlists CasperJS动态选择列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM