閱覽器智慧軟件

軟件 智慧生活

介紹幾款好用的庫 (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

圖片描述

圖片描述

相關文章
相關標籤/搜索