簡體   English   中英

更改查詢參數在 REST GET API 調用中引發 405 錯誤

[英]Changing query parameters throws 405 ERROR in REST GET API Call

I am trying to simulate via Python the REST API calls this website makes while calling for a typical security (try these selections: Get Historical Data for: "Security-wise Price Volume and Deliverable Positions of Data", Enter Symbol: "LaurusLabs", Select 系列:“全部”,Select 時間段:22-01-2021 至 10-02-2021)

這是我的代碼和修補的結果:

```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)

```

問題是每當我嘗試查詢不同的日期范圍時,結果似乎都會給出狀態 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”,所以我只是復制了盡可能多的標題來匹配。 我想可能是請求類型或日期格式,但事實並非如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM