开盘啦股票所属概念数据爬取

def kpl_stock_concept():
    url = 'https://pchq.kaipanla.com/w1/api/index.php'
    concept_label = []
    label_df = pd.DataFrame()
    

    # 获取概念ID
    for i in range(0,500,5):
        param1 = {'c': 'PCArrangeData', 'a': 'GetZSIndexPlate', 'SelType': 2, 'ZSType': 5, 'PType': 2, 'POrder': 1,
                       'PStart': '', 'PEnd': '', 'PIndex': i, 'Pst': 15, 'UserID': '399083','Token': '2292739880d01bd81e169e90a1898ebe'}

        html1 = json.loads(session.post(url=url, headers={'User-Agent': random.choice(ua_list)}, data=param1).text)
        if len(html1['plates']['list']) != 0:
            label_df = label_df.append(html1['plates']['list'],ignore_index=True)
        else:
            break

    label_df = label_df.iloc[:,0:2]
    label_df.columns = ['label','concept']

    # 获取概念股明细
    stock_df = pd.DataFrame()
    for label in label_df['label'].tolist():
        for j in range(0,500,5):
            param2 = {'c': 'PCArrangeData', 'a': 'GetZSIndexPlate', 'SelType': 3, 'LType': 6, 'LOrder': 1,
                             'LStart': '', 'LEnd': '', 'LIndex': j, 'Lst': 15, 'PlateID': label, 'UserID': '399083',
                             'Token': '2292739880d01bd81e169e90a1898ebe'}

            html2 = json.loads(session.post(url=url, headers={'User-Agent': random.choice(ua_list)}, data=param2).text)
            print(html2)
            if len(html2['stocks']['list']) != 0:
                data = pd.DataFrame(html2['stocks']['list'])
                print(data)
                data['label'] = label
                stock_df = stock_df.append(data, ignore_index=True)
            else:
                break

    stock_df = stock_df.iloc[:, [0, 1, 13]]
    stock_df.columns = ['scode', 'sname','label']

    stock_concept_detail_df = pd.merge(stock_df, label_df)

    stock_concept_detail_df.rename({'symbol': 'scode', 'name': 'sname', '板块': 'concept'}, axis='columns', inplace=True)

    stock_concept_detail_df = stock_concept_detail_df[['scode', 'sname', 'concept']]
    stock_concept_detail_df['scode'] = stock_concept_detail_df['scode'].apply(lambda x: (x + ".SH") if x.startswith('6') == True else (x + ".SZ"))
    stock_concept_detail_df.drop_duplicates(inplace=True)

    pandas_data_to_replace_handler(stock_concept_detail_df, 'stock_concept')

    #print(stock_concept_detail_df.head(5))
    print('开盘啦概念数据已更新')

 

posted @ 2020-08-20 21:18  麦小秋  阅读(3952)  评论(0编辑  收藏  举报