1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| from bs4 import BeautifulSoup import pandas as pd import requests import re import json import time
codes = [{"id": 1, "name": "创业板指", "zs": "399006", "cn": "159915", "cw": "161022"}, {"id": 2, "name": "深证成指", "zs": "399001", "cn": "159903", "cw": "202017"}, {"id": 3, "name": "深证100", "zs": "399330", "cn": "159901", "cw": "110019"}, {"id": 4, "name": "央视50", "zs": "399550", "cn": "159965", "cw": "217027"}, {"id": 5, "name": "沪深300", "zs": "000300", "cn": "510300", "cw": "160706"}, {"id": 6, "name": "上证180", "zs": "000010", "cn": "510180", "cw": "519180"}, {"id": 7, "name": "上证50", "zs": "000016", "cn": "510050", "cw": "001051"}, {"id": 8, "name": "中证100", "zs": "000903", "cn": "512910", "cw": "240014"}, {"id": 9, "name": "中小300", "zs": "399008", "cn": "159907", "cw": "270026"}, {"id": 10, "name": "创业板50", "zs": "399673", "cn": "159949", "cw": "007464"}, {"id": 11, "name": "中证1000", "zs": "000852", "cn": "512100", "cw": ""}, {"id": 12, "name": "中证500", "zs": "000905", "cn": "510500", "cw": "160119"}, {"id": 13, "name": "科创50", "zs": "000688", "cn": "588000", "cw": "011609"}, {"id": 14, "name": "深证红利", "zs": "399324", "cn": "159905", "cw": "481012"}, {"id": 15, "name": "50AH优选", "zs": "950090", "cn": "", "cw": "501050"}, {"id": 16, "name": "中证红利", "zs": "000922", "cn": "515890", "cw": "100032"}, {"id": 17, "name": "深证F120", "zs": "399702", "cn": "159910", "cw": "070023"}, {"id": 18, "name": "红利指数", "zs": "000015", "cn": "510880", "cw": "016441"}, {"id": 19, "name": "300红利", "zs": "000821", "cn": "512530", "cw": ""}, {"id": 20, "name": "红利低波", "zs": "H30269", "cn": "512890", "cw": "005561"}, {"id": 21, "name": "基本面50", "zs": "000925", "cn": "512750", "cw": "160716"}, {"id": 22, "name": "深证价值", "zs": "399348", "cn": "159913", "cw": "519706"}, {"id": 23, "name": "深证F60", "zs": "399701", "cn": "159916", "cw": "530015"}, {"id": 24, "name": "300价值", "zs": "000919", "cn": "", "cw": "519671"}, {"id": 25, "name": "红利潜力", "zs": "H30089", "cn": "515570", "cw": "007671"}, {"id": 26, "name": "180价值", "zs": "000029", "cn": "510030", "cw": "240016"}, {"id": 27, "name": "500SNLV", "zs": "930782", "cn": "512260", "cw": "003318"}, {"id": 28, "name": "300红利LV", "zs": "930740", "cn": "", "cw": "007605"}, {"id": 29, "name": "国证地产", "zs": "399393", "cn": "", "cw": "160218"}, {"id": 30, "name": "环境治理", "zs": "399806", "cn": "", "cw": "164908"}, {"id": 31, "name": "基建工程", "zs": "399995", "cn": "", "cw": "165525"}, {"id": 32, "name": "全指材料", "zs": "000987", "cn": "159944", "cw": ""}, {"id": 33, "name": "养老产业", "zs": "399812", "cn": "", "cw": "000968"}, {"id": 34, "name": "中国互联网", "zs": "H11136", "cn": "164906", "cw": "164906"}, {"id": 35, "name": "国证有色", "zs": "399395", "cn": "160221", "cw": ""}, {"id": 36, "name": "中国互联网50", "zs": "H30533", "cn": "513050", "cw": "006327"}, {"id": 37, "name": "中证银行", "zs": "399986", "cn": "512800", "cw": "001594"}, {"id": 38, "name": "全指金融", "zs": "000992", "cn": "159940", "cw": "001469"}, {"id": 39, "name": "全指工业", "zs": "000988", "cn": "159953", "cw": ""}, {"id": 40, "name": "家用电器", "zs": "930697", "cn": "159996", "cw": "005063"}, {"id": 41, "name": "国证食品", "zs": "399396", "cn": "159843", "cw": "160222"}, {"id": 42, "name": "高铁产业", "zs": "399807", "cn": "", "cw": "160135"}, {"id": 43, "name": "中证酒", "zs": "399987", "cn": "512690", "cw": "160632"}, {"id": 44, "name": "新能源", "zs": "000941", "cn": "516160", "cw": "012831"}, {"id": 45, "name": "中证消费", "zs": "000932", "cn": "159928", "cw": "000248"}, {"id": 46, "name": "证券公司", "zs": "399975", "cn": "512000", "cw": "004069"}, {"id": 47, "name": "中证国防", "zs": "399973", "cn": "512670", "cw": "502003"}, {"id": 48, "name": "全指可选", "zs": "000989", "cn": "159936", "cw": "001133"}, {"id": 49, "name": "全指信息", "zs": "000993", "cn": "159939", "cw": "000942"}, {"id": 50, "name": "中证军工", "zs": "399967", "cn": "512660", "cw": "161024"}, {"id": 51, "name": "大宗商品", "zs": "000979", "cn": "", "cw": "161715"}, {"id": 52, "name": "汽车指数", "zs": "931008", "cn": "", "cw": "004854"}, {"id": 53, "name": "中证煤炭", "zs": "399998", "cn": "013275", "cw": "161032"}, {"id": 54, "name": "CS食品饮", "zs": "930653", "cn": "", "cw": "001631"}, {"id": 55, "name": "中证传媒", "zs": "399971", "cn": "512980", "cw": "004752"}, {"id": 56, "name": "大农业", "zs": "399814", "cn": "", "cw": "001027"}, {"id": 57, "name": "中证白酒", "zs": "399997", "cn": "", "cw": "161725"}, {"id": 58, "name": "CS新能车", "zs": "399976", "cn": "515030", "cw": "161028"}, {"id": 59, "name": "中证环保", "zs": "000827", "cn": "512580", "cw": "001064"}, {"id": 60, "name": "科技龙头", "zs": "931087", "cn": "515000", "cw": "007873"}, {"id": 61, "name": "人工智能", "zs": "931071", "cn": "515980", "cw": "008020"}, {"id": 62, "name": "CS创新药", "zs": "931152", "cn": "159992", "cw": "012738"}, {"id": 63, "name": "5G通信", "zs": "931079", "cn": "515050", "cw": "008086"}, {"id": 64, "name": "光伏产业", "zs": "931151", "cn": "515790", "cw": "011102"}, {"id": 65, "name": "国证芯片", "zs": "980017", "cn": "159995", "cw": "008887"}, {"id": 66, "name": "中华半导体芯片", "zs": "990001", "cn": "512760", "cw": "008281"}, {"id": 67, "name": "恒生指数", "zs": "HSI", "cn": "159920", "cw": "164705"}, {"id": 68, "name": "恒生综合中小型股指数", "zs": "HSMSI", "cn": "", "cw": "160922"}, {"id": 69, "name": "恒生中国企业指数", "zs": "HSCEI", "cn": "510900", "cw": "110031"}, {"id": 70, "name": "恒生A股行业龙头指数", "zs": "HSCAIT", "cn": "", "cw": "540012"}, {"id": 71, "name": "标普500", "zs": ".INX", "cn": "513500", "cw": "050025"}, {"id": 72, "name": "纳斯达克100指数", "zs": ".NDX", "cn": "", "cw": "160213"}]
def scrape_website(api, headers=None): """获取网站数据""" try: response = requests.get(api, headers=headers, timeout=10) response.raise_for_status() return response except requests.RequestException as e: print(f"请求数据时出错: {e}") return None
def zswd(): """从API爬取指数温度数据""" t_results = scrape_website('http://caf-qibei.com/index?type=html') if t_results is None: return []
soup = BeautifulSoup(t_results.content, 'html.parser') tr_tags = soup.find_all('tr')
index_T = [] for tr in tr_tags: a_tags = tr.find_all('a', href=True) td_tag = tr.find('td', {'colspan': '3'})
if len(a_tags) >= 2 and td_tag: first_a_text = a_tags[0].text.strip() td_text = td_tag.text.strip()
try: td_float = float(td_text) index_T.append((first_a_text, td_float)) except ValueError: continue return index_T
wendu = zswd()
zs = [i['zs'] for i in codes]
df_wendu = pd.DataFrame(wendu, columns=['zs', 'temperature'])
df_filtered = df_wendu[df_wendu['zs'].isin(zs)]
df_codes = pd.DataFrame(codes)
df_merged = pd.merge(df_filtered, df_codes[['zs', 'name', 'cn', 'cw']], on='zs', how='inner')
df_merged = df_merged.rename(columns={'zs': '指数', 'name': '名称', 'temperature': '温度', 'cn': '场内', 'cw': '场外'})
df_merged_sorted = df_merged.sort_values(by='温度', ascending=True)
print(df_merged_sorted)
|