使用下面的代碼作為示例來獲取我想要的 res 變數
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_argument("--disable-blink-features=AutomationControlled")
#options.headless = True
driver = webdriver.Chrome('C:/Users/Amanr/Downloads/chromedriver_win32 v106/chromedriver', options=options)
driver.get("https://www.")
time.sleep(5)
res = driver.execute_script('''window.open('https://',"_blank");''')
main_window = driver.window_handles[1]
driver.switch_to.window(main_window)
ps = driver.page_source
print(ps)
我現在得到的結果是通過頁面源,但我希望它是清晰的 JSON 回應。它也沒有像這樣的鏈接中的完整資料:
{“識別符號”:“OPTIDXBANKNIFTY27-10-2022CE41200.00”,“名稱”:“BANKNIFTY”,“grapthData”:[[1666689300000,359.2],[1666689301000,386.35],[1666689302000,3903.45],00676689363 ],[1666689304000,385.8],[1666689305000,383.25],[16666689306000,378.95],[16666689307000,370.45] [1666689312000,400.9],[1666689313000,393.85],[1666689314000,402.7],[1666689315000,397.2],[1666689316000,391.5],[1666689317000,391.95],[1666689318000,391.85],[1666689319000,385.4],[1666689350000 ,380.55],[1666689351000,380.25],[1666689352000,377.15],[1666689353000,376.1],[1666689354000,373.85],[1666689355000,370
有什么方法可以讓我直接得到 json 的回應。
uj5u.com熱心網友回復:
您在上面的評論中確認的 api 端點回傳以下資訊 - 見下文:
import requests
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)
big_list = []
url = 'https://www.nseindia.com/api/chart-databyindex?index=OPTIDXBANKNIFTY27-10-2022CE41200.00'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.47'
}
s = requests.Session()
s.headers.update(headers)
s.get('https://www.nseindia.com/option-chain')
r = s.get(url)
for x in r.json()['grapthData']:
big_list.append(set(x))
df = pd.DataFrame(big_list, columns = ['Timestamp', 'Value'])
print(df)
結果在終端:
Timestamp Value
0 1666689300000 359.20
1 1666689301000 386.35
2 1666689302000 393.45
3 1666689303000 397.05
4 1666689304000 385.80
... ... ...
7691 1666711732000 121.85
7692 1666711733000 121.55
7693 1666711734000 121.20
7694 1666711735000 121.15
7695 1666711736000 121.10
7696 rows × 2 columns
我隨機命名了這些列,這些資料可能意味著別的東西,盡管如此。
uj5u.com熱心網友回復:
您可以從 API 中獲取所有需要的 json 格式的資料,如下所示:
import requests
api_url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
}
req = requests.get(api_url,headers=headers).json()
print(req.get('filtered').get('data'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/519940.html
標籤:Pythonjson硒
