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
| import requests from bs4 import BeautifulSoup
def safe_request(url, session, is_json=True): try: response = session.get(url) response.raise_for_status() return response.json() if is_json else response.text except requests.RequestException as e: print(f"Error requesting '{url}': {e}") return None
def extract_fund_data(raw_data, keys, code_mappings): code_set = {mapping['cn'] for mapping in code_mappings} return [ {key: float(item['cell'][key].rstrip('%')) if key != 'fund_id' else item['cell'][key] for key in keys if key in item['cell']} for item in raw_data if 'cell' in item and item['cell']['fund_id'] in code_set ]
def update_fund_codes(urls, session, code_mappings, keys): for url in urls: raw_data = safe_request(url, session) if raw_data: cleaned_data = extract_fund_data(raw_data.get('rows', []), keys, code_mappings) for data_item in cleaned_data: for code_mapping in code_mappings: if code_mapping['cn'] == data_item['fund_id']: code_mapping.update({k: data_item[k] for k in keys[1:]}) code_mapping['return'] = code_mapping.pop('increase_rt') code_mapping['yield'] = code_mapping.pop('discount_rt')
def get_index_temps(api, session): html_content = safe_request(api, session, is_json=False) if html_content is None: return []
soup = BeautifulSoup(html_content, 'html.parser') tr_tags = soup.find_all('tr') index_temps = [(tr.find_all('a', href=True)[0].text.strip(), float(tr.find('td', {'colspan': '3'}).text.strip())) for tr in tr_tags if len(tr.find_all('a', href=True)) >= 2 and tr.find('td', {'colspan': '3'}) and tr.find('td', {'colspan': '3'}).text.strip().replace('.', '', 1).isdigit()] return index_temps
def integrate_temps_with_funds(temperatures, code_mappings): id_to_code = {code['id']: code for code in code_mappings} for index_id, temp in temperatures: if index_id in id_to_code: id_to_code[index_id]['temp'] = temp return sorted([code for code in code_mappings if 'temp' in code], key=lambda x: x['temp'])
def filter_funds(funds, condition): return [{'名称': fund['name'], '代码': fund['cw'], '温度': fund['temp'], '涨跌': fund['return']} for fund in funds if condition(fund)]
session = requests.Session() urls = [ 'https://www.jisilu.cn/data/etf/etf_list/?___jsl=LST___t=1663138938775&rp=25&page=1', 'https://www.jisilu.cn/data/qdii/qdii_list/A?___jsl=LST___t=1665370506235&rp=22&page=1', 'https://www.jisilu.cn/data/qdii/qdii_list/E?___jsl=LST___t=1665371145110&rp=22&page=1' ] keys = ['fund_id', 'price', 'increase_rt', 'discount_rt'] code_mappings = [ {'id': '000016', 'name': '上证50', 'cn': '510050', 'cw': '001051'}, {'id': '000300', 'name': '沪深300', 'cn': '510300', 'cw': '160706'}, {'id': '000905', 'name': '中证500', 'cn': '510500', 'cw': '160119'}, {'id': '000922', 'name': '中证红利', 'cn': '515890', 'cw': '100032'}, {'id': 'SPX', 'name': '标普500', 'cn': '513500', 'cw': '050025'}, {'id': 'GDAXI', 'name': '德国DAX', 'cn': '513030', 'cw': '000614'}, {'id': '931079', 'name': '5G通信', 'cn': '515050', 'cw': '008086'}, {'id': '980017', 'name': '国证芯片', 'cn': '159995', 'cw': '008887'}, {'id': '399975', 'name': '证券公司', 'cn': '512000', 'cw': '004069'}, {'id': 'H30533', 'name': '互联网50', 'cn': '513050', 'cw': '006327'}, {'id': '399967', 'name': '中证军工', 'cn': '512660', 'cw': '161024'}, {'id': '399987', 'name': '中证酒', 'cn': '512690', 'cw': '160632'}, {'id': '399396', 'name': '国证食品', 'cn': '159843', 'cw': '160222'}, {'id': '399998', 'name': '中证煤炭', 'cn': '515220', 'cw': '161032'} ]
update_fund_codes(urls, session, code_mappings, keys) temperatures_api = 'http://caf-qibei.com/index?type=html' temps = get_index_temps(temperatures_api, session) funds = integrate_temps_with_funds(temps, code_mappings)
low_temp_funds = filter_funds(funds, lambda x: x['return'] < -1) high_temp_funds = filter_funds(funds, lambda x: x['return'] > 1)
print(low_temp_funds) print(high_temp_funds)
|