import pandas as pd
import requests

headers = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cookie': 'cookiesu=181695108116155; device_id=46ec772dfdf04c691b8e58a2b65c3c1a; s=cb12c0lym8; u=181695108116155; Hm_lvt_1db88642e346389874251b5a1eded6e3=1701745994; xq_a_token=a97fa15a5bb947c53ed434a6c0364dd03f36962c; xqat=a97fa15a5bb947c53ed434a6c0364dd03f36962c; xq_r_token=457987e3f3df9d22b53ad50b975087ff84ee9a79; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTcwNDU4NzkwNCwiY3RtIjoxNzAyMzQ4NjQzOTYxLCJjaWQiOiJkOWQwbjRBWnVwIn0.h0h-Jt7KL4s9FcnOeRJcDU6vlIg4DfKt32jlL9fMloWP3ZywcTZ9MqWx2ij3AJR1qgqnBUiAcRZ2_reBImFfQtusEZ_-ns23zey-PyCeY2wOLoXvOGq0yUdKqYKGzlJTYgiXLTTbZnblVZ0YkikIYGHpCROv-xt_n0b5s1abRJUXgI_uOU35a7JTh_JAPIxHXKI2VxPdibEAPjI0HBOJMVKPpUlQoGmuiI_vtQ-HdGpRODNsxSuS2QywKQvP6Zgr-h7VIVACUO76cN40kv9q3zY3-_Ce_VlHCEtsrjPoh0nsjfVy1sL_DgNa6uMk4SIZqtVHiFEuw0zKIFknLEMRAA; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1702364350',
        'Origin': 'https://xueqiu.com',
        'Referer': 'https://xueqiu.com/snowman/S/SZ301380/detail',
        'Sec-Ch-Ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
        'Sec-Ch-Ua-Mobile': '?0',
        'Sec-Ch-Ua-Platform': '"Windows"',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'
    }

df_1 = pd.read_excel('./需要补全的数据.xlsx', dtype=str)
for j in range(len(df_1)):
    code = df_1['A股代码'][j]
    code1 = str(code)
    while True:
        if len(code1)<6:
            code1 = '0'+code1
        else:
            break
    if code1[0] == '0' or code1[0] == '3':
        com_code = 'SZ'+code1
    elif code1[0] == '6':
        com_code = 'SH'+code1
    elif code1[0] == '8' or code1[0] == '4':
        com_code = 'BJ'+code1
    url = f'https://stock.xueqiu.com/v5/stock/f10/cn/company.json?symbol={com_code}'
    request = requests.get(url=url, headers=headers)
    json_data = request.json()
    info = json_data['data']['company']
    controlling_person =info['actual_controller']
    arear = info['provincial_name']
    # com_name = info['org_name_cn']
    # english_name = info['org_name_en']
    # short_name = info['org_short_name_cn']
    # website = info['org_website']
    # com_type = info['classi_name']
    # frdb = info['legal_representative']

    df_1['实控人'][j] = str(controlling_person)
    # df_1['所属区域'][j] = str(arear)
    df_1.to_excel('./需要补全的数据.xlsx', index=False)