从www.example.com中提取SBIN股票的最大痛苦值的Python代码https://www.niftytrader.in/stock-options-chart/sbin不起作用-我错过了什么?

vsdwdz23  于 2023-05-23  发布在  Python
关注(0)|答案(1)|浏览(118)

我希望Python代码从URL https://www.niftytrader.in/stock-options-chart/sbin刮SBIN股票的最大痛苦值
我的密码是。

import requests
from bs4 import BeautifulSoup

url = "https://www.niftytrader.in/stock-options-chart/sbin"

response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")

max_pain_element = soup.find("span", class_="fs-3 label-color-3")

if max_pain_element:
    # Extract the Max Pain value
    max_pain = max_pain_element.text.strip()
    print("Max Pain:", max_pain)
else:
    print("Max Pain value not found on the webpage.")

但是,我得到的输出为“网页上未找到最大疼痛值”

ivqmmu1c

ivqmmu1c1#

您在页面上看到的数据是通过JavaScript从外部URL加载的,因此beautifulsoup看不到它。要将所有数据放入dataframe中,您可以使用下一个示例:

import requests
import pandas as pd

api_url = 'https://h9cg992bof.execute-api.ap-south-1.amazonaws.com/webapi/symbol/psymbol-list'

data =requests.get(api_url).json()
df = pd.DataFrame(data['resultData'])
print(df)

图纸:

symbol_name  today_close  prev_close  max_pain  lot_size
0      AARTIIND       494.70      495.65     500.0     850.0
1           ABB      3894.75     3897.50    3700.0     250.0
2    ABBOTINDIA     20898.45    20970.55   21000.0      40.0
3     ABCAPITAL       163.90      164.80     165.0    5400.0
4         ABFRL       190.85      193.70     200.0    2600.0
5           ACC      1729.05     1712.45    1760.0     250.0
6      ADANIENT      1956.05     1890.00    1900.0     250.0
7    ADANIPORTS       688.10      664.95     690.0     625.0
8         ALKEM      3302.10     3326.30    3400.0     200.0

...

仅获取SBIN值:

print(df[df.symbol_name == 'SBIN'].iloc[0]['max_pain'])

图纸:

580.0

相关问题