1 – 获得上市公司股票清单

我在WordPress中创建了一个自定义文章类型(Custom Post Type),名为·ST_Stock,旨在保存所有上市公司的股票信息。数据获取如下。

访问上海证券交易所网站,具体的URL如下:http://www.sse.com.cn/assortment/stock/areatrade/area/detail.shtml?eqid=e272d792000000ef00000004642e7fd7

从上述网页中,我找到了包含上市公司信息的表格,并将其内容保存至名为list.html

使用以下Python脚本,将其转换为JSON格式,保存在list.json中。

import pandas as pd
from html import unescape
import json


with open('stock_1.html', 'r', encoding='utf-8') as file:
    html_string = file.read()

    # to Excel
    dfs = pd.read_html(unescape(html_string))
    df = dfs[0]
    df.to_excel('stock_1.xlsx', index=False)


    # to JSON
    df.drop(columns=['A股代码'], inplace=True)
    df.drop(columns=['B股代码'], inplace=True)
    column_map = {
        '上市公司代码': 'code',
        '上市公司名称': 'title'
    }
    df = df.rename(columns=column_map)

    data = df.to_dict('records')
    json_string = json.dumps(data, indent=4, ensure_ascii=False)
    with open('stock_1.json', 'w', encoding='utf-8') as json_file:
        json.dump(data, json_file, ensure_ascii=False, indent=4)

就可以准备导入到WordPress中。