Node学习记录: 写脚本工具

简单的搜索脚本

安装 nightmare
安装 babel-register
安装 babel-polyfill

const Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });

const run = async () => {
  nightmare.goto('http://www.baidu.com')
}

这里的goto是异步执行的,会返回一个promise
前面加个await 使它以同步的方式执行

index.js

require('babel-register');
require('babel-polyfill');
require('./run')

run.js



const Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });

const run = async () => {
  await nightmare.goto('http://www.baidu.com')
  await nightmare.type('#kw','海贼王')
  await nightmare.click("#su")
}


run()

图片描述

图片描述

自动登录cnode论坛

新建一个config.js文件


export default {
  username: "AlexZ33",
  password: "自己的密码"

}

run.js变成


const Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });
import config from './config.js'

const run = async () => {
  await login()
}




const login = async () =>{
  await nightmare.goto('https://cnodejs.org/signin')
  await nightmare.wait('#signin_form')

  await nightmare.click('.form-actions :nth-child(2)')
  await nightmare.wait('#login_field')

  await nightmare.type('#login_field', config.username)
  await nightmare.type('#password',config.password)

  await nightmare.click('input[name="commit"]')
}

run()

图片描述

nodejs论坛灌水小工具

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值