无法在Ruby中获取URL / Cookies,但可以在Python中使用

问题描述

我在通过ruby访问URL时遇到问题,但是它正在与python的requests库一起使用。 这就是我正在做的事情,该链接https://www.nseindia.com/get-quotes/derivatives?symbol=SBIN我想访问并开始使用它,然后需要在同一会话中点击链接https://www.nseindia.com/api/option-chain-equities?symbol=SBIN'this的回答对我有很大帮助但是我需要用红宝石来做。我已经尝试过rest-client,net/http,httparty,httpclient,即使我只是在做

require 'rest-client'
request = RestClient.get 'https://www.nseindia.com/get-quotes/derivatives?symbol=SBIN' 

它无限期地无响应,我也用headers尝试了同样的事情,但是在无限时间内仍然无响应。
任何帮助,将不胜感激。
谢谢。

解决方法

您是否可以确认RestClient是否适用于其他网址,例如google.com?

require 'rest-client'
RestClient.get "https://www.google.com"

对于它的价值,我能够通过RestClient向Google发送成功的get请求,但不能使用您提供的网址。但是,我可以通过在标题中指定User-Agent来获得响应:

require 'rest-client'

RestClient.get "https://www.nseindia.com/api/option-chain-equities?symbol=SBIN%27"
=> Hangs...

RestClient.get "https://www.nseindia.com/api/option-chain-equities?symbol=SBIN%27",{"User-Agent": "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"}
=> RestClient::Unauthorized: 401 Unauthorized

如果您要从api获取任何有用的数据,我认为需要进行一些身份验证。