在pthon中如何实现pine语言中request.security的方法

在量化开发中碰到一个棘手问题。
在tradingview自带pine语言中,可以使用request.security方法。
请教各位达人,如何在python中实现呢?万分感谢

你可以使用 Python Requests 库来模拟 Pine 脚本中的 request.security() 方法。

import requests

# 定义证券的名称
symbol = "AAPL"

# 设置请求头
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}

# 发送请求
response = requests.get(f"https://finance.yahoo.com/quote/{symbol}", headers=headers)

# 检查响应状态码
if response.status_code == 200:
    print(response.text)
else:
    print("请求失败!")