阅览器智慧软件

软件 智慧生活

介绍几款好用的库 (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 上了解这个库 git

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

安装 插件

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(()=>{})当中也能够放置函数(例如能够判断目标文件是否存在的函数)npm

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

这是我作的一个翻译的小程序 json

图片描述

图片描述

相关文章
相关标签/搜索