更改查询参数在REST GET API调用中引发405错误



我试图通过Python模拟此网站在调用典型安全性时进行的REST API调用(尝试以下选择:获取历史数据:"安全性-价格总量和数据的可交付位置",输入符号:"LaurusLabs",选择系列:"全部",选择时间段:2021年1月22日至2021年2月10日(

这是我的代码和修补的结果:

```python
import requests
from pprint import PrettyPrinter as pp
import urllib
printer = pp(indent=0)
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
"Dnt": "1",
"Connection": "keep-alive",
"Host": "www1.nseindia.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"}
session = requests.Session()
url_symb_count = "https://www1.nseindia.com/marketinfo/sym_map/symbolCount.jsp?symbol=LAURUSLABS"
response = requests.get(url_symb_count, headers = headers)
cookies = dict(response.cookies)
x = (response.text).strip()
#printer.pprint(cookies)
f = { "symbol": "LAURUSLABS","segmentLink": "3","symbolCount": 1,"series": "ALL","dateRange": " ","fromDate": "20-01-2021","toDate": "10-02-2021","dataType": "PRICEVOLUMEDELIVERABLE"}
url = 'https://www1.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?' + urllib.parse.urlencode(f)
response = session.get(url, headers = headers, cookies=cookies)
x = (response.text)
printer.pprint(x)
printer.pprint(url)
```

问题是,每当我试图查询不同的日期范围时,结果似乎是显示Status 405错误。事实上,这个链接似乎只适用于这个选择,但它让我无法理解如何或为什么?

你能帮我解释一下为什么直接操纵日期会给我不同的结果吗?

所以我已经玩了半个小时了,每当我在f字典中从和到日期更改时,它似乎一直在工作。


import requests
from pprint import PrettyPrinter as pp
import urllib
printer = pp(indent=2)
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"Connection": "keep-alive",
"Dnt": "1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
"Host": "www1.nseindia.com",
"Referer": "https://www1.nseindia.com/products/content/equities/equities/eq_security.htm",
"Sec-GPC": "1",
"X-Requested-With": "XMLHttpRequest"
}
f = {
"symbol": "LAURUSLABS",
"segmentLink": "3",
"symbolCount": 1,
"series": "ALL",
"dateRange": " ",
"fromDate": "04-05-2020",
"toDate": "02-05-2021",
"dataType": "PRICEVOLUMEDELIVERABLE"
}
url = 'https://www1.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?' + urllib.parse.urlencode(f)
response = requests.get(url, headers=headers)
x = (response.text)
printer.pprint(x)

编辑解释:

我只是在浏览OP提供的链接时观察网络选项卡,并注意到所有请求都来自";eq_security.htm",所以我只是复制了尽可能多的标题来匹配。我想可能是请求类型或日期格式,但事实并非如此。

最新更新