无法将 JSON 响应打印或存储在变量Selenium python 中



以下面的代码为例,在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响应。它也没有链接中的完整数据像这样的东西:

{"identifier":"OPTIDXBANKNIFTY27-10-2022CE41200.00","name":"BANKNIFTY","grapthData":[[1666689300000359.2],[11666689301000386.35],[1666689302000393.45],[16616689303000397.05],[1166689304000385.8],[1661689305000383.25],[16689306000378.95],[166689307000370.45],[16636689308000369.15],[16669309000372.3],[16669310000383.75],[166689311000391.65]、[166689312000400.9],[1666689313000393.85],[166668934000402.7],[166689315000397.2],[16616689316000391.5],[11666689317000339.95],[16689318000391.85],[11666689319000385.4],[1666689350003380.55],[1166689351000380.25],[16636689352000377.15],[16676899353000376.1],[16616899354000373.85],[166 6899355000370

有什么方法可以直接获得json的响应吗?

您可以从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'))

您在上面的注释中确认的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

我随机命名了这些列,这些数据可能意味着其他东西,尽管如此,它还是存在的

最新更新