nodejs爬虫,POST请求发送Request Playload格式数据

nodejs爬虫,POST请求发送Request Playload格式数据。

本文以网易云课堂其中2个POST请求为例,使用request模块实现。

例子1

1、地址:

http://study.163.com/dwr/call/plaincall/MongoAttributesBean.obtainByRandomWithLimitTime.dwr?1521595046314

2、请求截图


从上图可以看到"Content-Type"为"text/plain";数据发送方式:Request Payload,

数据就是普通换行的字符串,而不是JSON字符串。

3、nodejs代码

  let request = require("request");
  let url = `http://study.163.com/dwr/call/plaincall/MongoAttributesBean.obtainByRandomWithLimitTime.dwr?${+new Date()}`;
  let body = `callCount=1\nscriptSessionId=\${scriptSessionId}190\nhttpSessionId=17faef42e6db4291819618913dc7f8da\nc0-scriptName=MongoAttributesBean\nc0-methodName=obtainByRandomWithLimitTime\nc0-id=0\nc0-param0=string:spjdt\nc0-param1=number:7\nbatchId=1521527406791\n`;
  let opts = {
    url,
    method: "POST",
    headers: { "Content-Type": "text/plain" },
    body: body
  }
  request(opts, (e, b, d) => {
    if (e) return console.log(e);
    console.log(d);
  })
4、运行结果


能正常获取返回的数据。

例子2

1、地址:http://study.163.com/p/search/studycourse.json
这是网易云课堂搜索课程的请求地址。
2、请求截图

"Content-Type"为"application/json";数据发送方式也是:Request Payload,
数据格式为标准的JSON字符串。
3、nodejs代码
  let request = require("request");
  let url = `http://study.163.com/p/search/studycourse.json`;
  let body = {
    activityId: 0,
    keyword: "nodejs",
    orderType: 5,
    pageIndex: 1,
    pageSize: 50,
    priceType: -1,
    relativeOffset: 0,
    searchTimeType: -1,
  };
  let opts = {
    url,
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body)
  }
  request(opts, (e, b, d) => {
    if (e) return console.log(e);
    console.log(d);
  })
4、运行结果

这个list就是搜索到的课程结果。

猜你喜欢

转载自blog.csdn.net/zzwwjjdj1/article/details/79625865
今日推荐