Ruby HTTP请求始终返回403响应(请求太多)。在邮递员/浏览器中工作

时间:2020-02-22 11:34:39

标签: ruby http

我正在尝试编写一个简单的函数,该函数可以轻松地从分类列表中提取联系人信息。

背景

我正在查看的URL是 https://www.idealista.pt/imovel/27542922/

浏览Chrome中的开发人员工具,我发现它向该URL发出了GET请求。 https://www.idealista.pt/pt/ajax/listingController/adContactInfoForListing.ajax?adId=27542922

如果我用Postman发出GET请求,或者只是将第二个URL复制到Chrome中,我会得到一个包含各种详细信息的JSON。

我的代码

(Ruby)

const checkAuth = async (ctx, isProtected) => {
    const { auth } = nextCookie(ctx);

    if (isProtected && !auth) {
        if (!isClient() && ctx.res) {
            ctx.res.writeHead(302, { Location: '/' });
            ctx.res.end();
        } else {
            await Router.push('/');
        }
    }

    return auth || null;
};

export const withAuth = (isProtected = false) => (WrappedComponent) => {
    const WrappedWithAuth = (props) => {
        const { token } = useSelector(
            state => state.user
        );

        useEffect(() => {
            if (isProtected && !token) Router.push('/');
        }, [token]);

        return <WrappedComponent {...props} />;
    };

    WrappedWithAuth.getInitialProps = async (ctx) => {
        const token = await checkAuth(ctx, isProtected);
        const { store } = ctx;

        if (token) {
            store.dispatch(userSetToken(token));

            try {
                const { data } = await UserService.getProfile();
                store.dispatch(userGetProfileSuccess(data));
            } catch (e) {
                store.dispatch(userGetProfileFailure(e));
            }
        }

        const componentProps =
            WrappedComponent.getInitialProps &&
            (await WrappedComponent.getInitialProps({ ...ctx, token }));

        return { ...componentProps };
    };

    return WrappedWithAuth;
};

问题

响应是一个403,上面有一个正文,表示系统已检测到在很短的时间内发出了许多请求。

我可以通过执行七个或八个连续的请求在Postman中复制此内容,但是如果我等一两分钟再尝试一次,我会回到看到JSON的位置。

通过Ruby,它立即发生。

我尝试过的事情

我尝试将Postman创建的部分或全部临时标头复制到我在Ruby中的请求中,但仍然收到相同的错误或404

  uri = URI('https://www.idealista.pt/pt/ajax/listingController/adContactInfoForListing.ajax?adId=27542922')
  foo = Net::HTTP.get(uri)
  JSON.parse(foo)

1 个答案:

答案 0 :(得分:-2)

你必须使用代理,并更改ip

相关问题