bigquant收益计算爬虫

Python
321
0
0
2022-04-22

bigquant平台的收益计算不太符合我的要求,根据需求完成了一个收益计算的爬虫,分享出来供大家参考

from requests.cookies import RequestsCookieJar
from selenium import webdriver
from time import sleep
import requests
import json
from selenium.webdriver.chrome.options import Options


def get_data(username, password, nid):
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    # 后面是你的浏览器驱动位置,记得前面加r'','r'是防止字符转义的
    driver = webdriver.Chrome('./chromedriver'
                              # , options=chrome_options
                              )
    # 用get打开百度页面
    driver.get("https://bigquant.com/live/strategy?notebook_id={}".format(nid))
    sleep(0.01)
    driver.find_element_by_class_name('login-link').click()
    sleep(0.01)
    driver.find_element_by_class_name('m-bigquant-input-item').send_keys(username)
    sleep(0.01)
    driver.find_element_by_xpath('//*[@id="app"]/div[2]/div/div[2]/div/div/input').send_keys(password)
    sleep(10)
    ccsz = int(
        driver.find_element_by_xpath('//*[@id="overview"]/div/section/div[2]/div/div[2]/ul/li[1]/span[1]').text[1:])
    kyzj = int(
        driver.find_element_by_xpath('//*[@id="overview"]/div/section/div[2]/div/div[2]/ul/li[2]/span[1]').text[1:])
    cszj = int(
        driver.find_element_by_xpath('//*[@id="overview"]/div/section/div[2]/div/div[2]/ul/div/span[1]').text[1:])

    cczb = driver.find_element_by_xpath('//*[@id="overview"]/div/section/div[2]/div/div[2]/ul/li[3]/span[1]').text
    ljsy = float(driver.find_element_by_xpath('//*[@id="overview"]/div/section/div[1]/li/span[1]').text[:-1])
    cookie_list = driver.get_cookies()
    cookies = RequestsCookieJar()
    for cookie in cookie_list:
        cookies.set(cookie['name'], cookie['value'])
    data = json.loads(requests.get(
        'https://bigquant.com/bigwebapi/algo_info/plot_risk?owner={}&notebook_id={}&limit=-1'.format(username.lower(),
                                                                                                     nid),
        cookies=cookies).content.decode())['data']['algo_info_plot_risk']

    ljsy = (kyzj + ccsz) / cszj - 1
    zrsy = round(float(data['cum_return_plot'][-2][1]), 4)

    dqjz = round((ccsz + kyzj) / cszj, 4)
    drjz = ljsy - zrsy
    dryk = round((ccsz + kyzj) * drjz, 2)

    name = data['algo_name']
    print("{}\n当前净值为:{}\n当日净增为:{}\n当日盈亏为:{}\n持仓占比为:{}\n".format(name, dqjz, str(round(drjz * 100, 2)) + "%",
                                                                dryk, cczb))
    # 关闭浏览器
    driver.quit()
if __name__ == '__main__':
    fnids = []
    for i in fnids:
        get_data("username", 'password', i)