bigquant收益計算爬蟲

SilenceHL發表於2021-06-12

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)
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章