【问题标题】:axios get the redirect URL of page returned by postaxios 获取post返回页面的重定向URL
【发布时间】:2021-08-09 22:42:25
【问题描述】:

我试图在从 Axios 重定向后获取网页的 URL,但 Axios 配置中似乎没有在重定向后给我预期的 URL。

我认为我传递了正确的搜索框参数,以获取我的输出 URL。但我无法确定,因为我看不到响应 URL。

我的目标——获取网址:https://www.redfin.com/GA/Lawrenceville/2105-Bentbrooke-Trl-30043/home/24906463

我的代码:

var querystring = require("querystring");
let axios = require("axios");

async function run() {
  let rf = await axios.post(
    "https://www.redfin.com",
    querystring.stringify({
      searchInputBox: "2105 bentbrooke trl",
    }),
    {
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
    }
  );
  console.log(`RF URL: `, rf.config, rf.request.res.responseUrl);
}
run();

我目前的输出:

RF URL:  {
  url: 'https://www.redfin.com',
  method: 'post',
  data: 'searchInputBox=2105%20bentbrooke%20trl',       
  headers: {
    Accept: 'application/json, text/plain, */*',        
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'axios/0.21.1',
    'Content-Length': 38
  },
  transformRequest: [ [Function: transformRequest] ],   
  transformResponse: [ [Function: transformResponse] ], 
  timeout: 0,
  adapter: [Function: httpAdapter],
  xsrfCookieName: 'XSRF-TOKEN',
  xsrfHeaderName: 'X-XSRF-TOKEN',
  maxContentLength: -1,
  maxBodyLength: -1,
  validateStatus: [Function: validateStatus]
} https://www.redfin.com/

类似的问题,但这里没有一个对我有用的建议答案(我认为 Axios 可能已经更新,或者由于我使用的异步调用而存在一些差异):how to get the landing page URL after redirections using axios

【问题讨论】:

    标签: node.js redirect axios


    【解决方案1】:

    您的 sn-p 正在按预期工作。它向带有表单头的 URL 发送 POST 请求,并根据 form 头以正确的形式发布数据。

    curl --request POST 'https://www.redfin.com/' --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'searchInputBox=2105 bentbrooke trl'
    

    但是每个网站都以不同的方式处理其表单。这不是本网站处理其搜索表单的方式。


    当我在网站上提交表单时进行了一些逆向工程,在浏览器的 devtools 网络选项卡中浏览了请求,然后在 Postman 中清理它们(以查找和删除不需要的参数)。

    该网站需要带有查询参数location=<your query>v=2(可能是搜索引擎的一个版本)的GET 请求。它还需要设置User-Agent 标头,否则返回验证码页面而不是预期结果。

    此请求返回一组两个 JSON 对象(第一个为空)。在这种情况下,第一个被忽略,只有第​​二个被他们的 JS 代码解析,如果可用,它会重定向到精确匹配。否则会显示结果列表。

    CURL 请求:

    curl 'https://www.redfin.com/stingray/do/query-location?location=2105%20bentbrooke%20trl&v=2' --header 'User-Agent: bot'
    

    返回数据:

    {}&&{
        "version": 380,
        "errorMessage": "Success",
        "resultCode": 0,
        "payload": {
            "sections": [
                {
                    "rows": [
                        {
                            "id": "1_24906463",
                            "type": "1",
                            "name": "2105 Bentbrooke Trl",
                            "subName": "Lawrenceville, GA, USA",
                            "url": "/GA/Lawrenceville/2105-Bentbrooke-Trl-30043/home/24906463",
                            "active": true,
                            "claimedHome": false,
                            "invalidMRS": false,
                            "businessMarketIds": [
                                14
                            ],
                            "countryCode": "US",
                            "searchStatusId": 2
                        }
                    ],
                    "name": "Addresses"
                }
            ],
            "exactMatch": {
                "id": "1_24906463",
                "type": "1",
                "name": "2105 Bentbrooke Trl",
                "subName": "Lawrenceville, GA, USA",
                "url": "/GA/Lawrenceville/2105-Bentbrooke-Trl-30043/home/24906463",
                "active": true,
                "claimedHome": false,
                "invalidMRS": false,
                "businessMarketIds": [
                    14
                ],
                "countryCode": "US",
                "searchStatusId": 2
            },
            "extraResults": {},
            "responseTime": 0,
            "hasFakeResults": false,
            "isGeocoded": false,
            "isRedfinServiced": false
        }
    }
    

    因此,当您转换 CURL 请求和响应解析并寻找与 Node.JS Axios 的完全匹配时,您会得到以下代码:

    const axios = require('axios');
    
    const BASE_URL = 'https://www.redfin.com';
    
    const getRedirectUrl = async (query) => {
        const response = await axios({
            'method': 'GET',
            'url': getSearchUrl(query),
            'headers': {
                'User-Agent': 'I\'m just a bot that passes the user-agent header',
            }
        });
    
        const result = getSecondObjectFromBody(response.data);
    
        if (result.payload.exactMatch === undefined) {
            return null;
        }
    
        return BASE_URL + result.payload.exactMatch.url;
    }
    
    const getSearchUrl = (query) => {
        return BASE_URL
            + '/stingray/do/query-location?location='
            + encodeURIComponent(query)
            + '&v=2';
    }
    
    const getSecondObjectFromBody = (body) => {
        return JSON.parse(body.slice(body.indexOf('&&') + 2));
    }
    
    const run = async () => {
        console.log(await getRedirectUrl('2105 bentbrooke trl')); // has exact match
        console.log(await getRedirectUrl('fdsfds')); // no exact match
    }
    
    run();
    

    哪些输出:

    https://www.redfin.com/GA/Lawrenceville/2105-Bentbrooke-Trl-30043/home/24906463
    null
    

    【讨论】:

      猜你喜欢
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 2011-06-08
      • 2016-03-26
      • 1970-01-01
      相关资源
      最近更新 更多