阅览器智慧软件

软件 智慧生活

介绍几款好用的库 (GitHub 地址 )想了解的可以自行查下看


1. https://github.com/ariya/phantomjs
2. https://github.com/laurentj/slimerjs
3. https://github.com/casperjs/casperjs
4. https://github.com/segmentio/nightmare

这里主要用了nightmare这个库 感兴趣的同学可以自行去github 上了解这个库

==nightmare==:目标是暴露模拟用户动作(如几个简单的方法goto,type和click),其中,感觉同步的脚本中的每个块,而不是深度嵌套的回调的API。它最初设计用于在没有API的站点之间自动执行任务,但最常用于UI测试和爬网

安装 插件

npm install -y //建立一个package.json的文件
npm install --save nightmare   //安装依赖库

插件的 引用简单介绍一下

这是文件的大纲可以根据自己的需求进行更改
import Nightmare from 'nightmare';
const nightmare = Nightmare({ show: true });

nightmare
  .goto('https://duckduckgo.com')
  .type('#search_form_input_homepage', 'github nightmare')
  .click('#search_button_homepage')
  .wait('#r1-0 a.result__a')

  .end()
  .then(console.log)
  .catch((error) => {
    console.error('Search failed:', error);
  });
  • .goto 链接要去的url
  • type模仿 寻找文本框添加自己要找到的文本框并加上需要的文本
  • click 模拟鼠标的点击事件 方法同上
  • wait
    检查.wait()条件成功之间等待多长时间

.wait(()=>{})当中也可以放置函数(例如可以判断目标文件是否存在的函数)

  • .end()结束文件
    也可以配合实用then使用
nightmare
  .goto(someUrl)
  .end(() => "some value")
  //打印 "some value"
  .then(console.log);

这是我做的一个翻译的小程序

《阅览器智慧软件》

《阅览器智慧软件》

    原文作者:黄黄黄
    原文地址: https://segmentfault.com/a/1190000011635431
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞