import requests, json, time, pymysql, sys
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver


headers = {
    'cookie': 'device_id=72922798c76cce710281dd6a2404a7a7; s=d11bic2en8; xq_a_token=7da3658c0a79fd9ef135510bc5189429ce0e3035; xqat=7da3658c0a79fd9ef135510bc5189429ce0e3035; xq_r_token=c4e290f788b8c24ec35bd4b893dc8fa427e1f229; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTY3OTk2MjkxMiwiY3RtIjoxNjc4NDk2MzMwMzY5LCJjaWQiOiJkOWQwbjRBWnVwIn0.eteAa4NvugN2Ck4dO4r5xnNKTW-HbFKYrT86auaexzd4ESpeWFAdxrYMjhC4nQevwGuFDb0hZWb2Xsk8DKIpw5O42CjvotTBCv8ij4hHTZ1HoR1tNMwlcb3iibklvdSaeYeF-4Mcp_sOY4BSzb3dcEDxUlwvPSpvU94aX5FGgubkVnXbbhrTQ05NxqumlqqULuqrc3U8tMCrkufef3sVNj4L_8TwbZBuYo58OEYF5zTWEyWVkvRswo9snE7Q242qghidhYpG34G04SSlmG6chyShNGLnV1hA5k3TI6ELLGPArEey7mxtpBcRpEp3jhou0tJWUqjHeyvqMvGxGbPwng; u=301678496364087; Hm_lvt_1db88642e346389874251b5a1eded6e3=1676337877,1677743855,1678191984,1678496365; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1678497585',
    # 'referer': 'https://xueqiu.com/snowman/S/SZ002931/detail',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
}
df_1 = pd.read_excel('./需要补全的数据.xlsx', dtype=str)
for j in range(len(df_1))[560:]:
    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

    print(com_code)
    url_dongfang = f'https://emweb.eastmoney.com/PC_HSF10/CompanySurvey/PageAjax?code={com_code}'
    json_dongfang = requests.get(url_dongfang).json()
    try:
        com_money = json_dongfang['fxxg'][0]['TOTAL_FUNDS']
    except:
        continue
    # 企业总市值
    if com_money:
        com_money = round(com_money, 4)
    df_1['上市总市值'][j] = str(com_money)
    df_1.to_excel('./需要补全的数据.xlsx', index=False)

