matthewmueller / x-ray

The next web scraper. See through the <html> noise.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"X-ray" how to post an Ajax request, I tried to use "request-X-ray ", but always failed, how to post data and how to accept the returned JSON data instead of HTML

chunfytseng opened this issue · comments

const makeDriver = require('request-x-ray')
const x = require('x-ray')()
const options = {
    method: 'POST',
    jar: true,
    headers: {
      "Host": "hotel.xxx.com",
      "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:80.0) Gecko/20100101 Firefox/80.0",
      "Accept": "application/json, text/plain, */*",
      "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
      "Accept-Encoding": "gzip, deflate, br",
      "Content-Type": "application/json;charset=utf-8",
      "Content-Length": "548",
      "Origin": "https://hotel.xxx.com",
      "Connection": "keep-alive",
      "Referer": "https://hotel.xxx.com/cn/vvv/q-ddd?fromDate=2020-09-08&toDate=2020-09-09&cityName=vvv"
    },
    json: true,
    body: JSON.parse('{"b":{"bizVersion":"17","cityUrl":"vvv","fromDate":"2020-09-08","toDate":"2020-09-09","q":"aaa","qFrom":3,"start":0,"num":20,"minPrice":0,"maxPrice":-1,"level":"","sort":0,"cityType":1,"fromForLog":1,"uuid":"","userName":"","userId":"","fromAction":"","searchType":0,"hourlyRoom":false,"locationAreaFilter":[{"params":"-1","reqParams":[{"param":"-1","title":"sss","secondShowSelected":false,"jsonExtra":"{}","suggestType":""}],"filterType":"POI_DISTANCE"}],"comprehensiveFilter":[],"channelId":1},"qrt":"h_hlist","source":"website"}'),
  }
  const driver = makeDriver(options)
  x.driver(driver)
  await x(event.url, "")
    .then(res => {
      console.log("res:" + JSON.stringify(res))
    })
    .catch(err => {
      console.err("err:" + JSON.stringify(err))
    })

I don’t think x-ray is right for this use case. You can requests on its own much more easily.