如何利用 C# 爬取「财报说」中的股票数据?

两年前在一个微信群中,听一些朋友讨论林明璋的“超级数字力”课程,后来也听 李笑来 推荐过他的课程。由于比较远,我只买了一本 林明璋 写的《用生活尝试就能看懂财务报表》来读。再后来发现了他们做的这个网站 “财报说”。从该网站上我们可以看到各支股票按照 申万行业分类 做成了列表;每只股票都提供了 4年 的资产负债表、现金流量表 和 利润表,并利用一套五个维度(现金流、营运能力、盈利能力、财务结构、偿债能力)的模型进行评分。

我以前在图文 赚钱是刚需,如何正确的交易股票? 中介绍过自己的选股策略,从 000015 红利指数 的十大权重股中选择哪些分红能力强,且处于低估位置的股票。既然 林明璋 把自己的知识做成了网站,为何不把这些数据爬下来也作为自己在下一个周期选股的参考呢?


好了,我们开始吧!

首先,我们看一下「财报说 - 行业板块」对应的网页。

网址为:

https://beta.caibaoshuo.com/cn_industries/3

原网页

其次,我们看一下“一级行业”、“二级行业”、“股票列表”部分对应的网页源码。

“一级行业”部分对应的网页源码如下

一级行业源码

通过网页的HTML DOM TREE找到第一个industries-header_section-lists类中的a标签,就可以得到一级行业的名称以及对应的网页地址。

“二级行业”部分对应的网页源码如下

二级行业源码

通过网页的HTML DOM TREE找到第二个industries-header_section-lists类中的a标签,就可以得到二级行业的名称以及对应的网页地址。

“股票列表”部分对应的网页源码如下

股票列表源码

通过网页的HTML DOM TREE找到table tbody tr标签(table 内部的 tbody 内部的 tr 标签,详见 CSS 的选择器语法)每一个 tr 对应表格中的一行数据,找到tr内部的td标签,就找到了希望获取的“股票名称”、“股票编号”以及显示股票详细信息的“网址”。

接着,我们爬取对应股票的详细数据。

如上图所示对应的网址为:

https://beta.caibaoshuo.com/companies/000858

股票详情页面

对应的源码为:

详情页源码

通过网页的HTML DOM TREE找到header-price类,在该类中找到valuepepb-ratioratio-lowratio-normalratio-high这些类,就可以得到“当前股价”、“当前市盈率”、“当前市净率”、“10倍市盈率”、“20倍市盈率”、“30倍市盈率”的数据。

最后,我们用 Jumony 这套开源代码来获取网页对应的 HTML DOM TREE ,这套开源代码可以在 Github 上下载。

下载地址为:

https://github.com/Ivony/Jumony

Jumony下载

这里对 Jumony 就不做过多介绍了,要是大家感兴趣,可以在图文下方留言,我后面再写几篇图文来介绍这个工具。


找到了所要爬取的网页地址,分析完网页的源码,确定了所用的工具和技术路线,剩下的就是写代码进行实现了。

Step1. 定义存储行业类型的结构 Industry

public class Industry
{
    /// <summary>
    /// 行业名称
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// 对应网址
    /// </summary>
    public string Url { get; set; }
}

Step2. 获取一级行业的列表 List<Industry>

private const string Url = "https://beta.caibaoshuo.com";

public static List<Industry> GetLevelOne()
{
    List<Industry> result = new List<Industry>();
    
    string url = Url + @"/cn_industries/3";
    IHtmlDocument doc = HtmlSpiter.GetHtmlDocument(url);
    if (doc == null)
        return result;

    List<IHtmlElement> lists = doc.Find(".industries-header__section-lists").ToList()[0].Find("a").ToList();

    for (int i = 0; i < lists.Count; i++)
    {
        Industry item = new Industry();
        item.Name = lists[i].InnerHtml();
        item.Url = Url + lists[i].Attribute("href").AttributeValue;
        result.Add(item);
    }
    return result;
}

Step3. 获取二级行业的列表 List<Industry>

public static List<Industry> GetLevelTwo(Industry levelOne)
{
    List<Industry> lstResult = new List<Industry>();

    IHtmlDocument doc = HtmlSpiter.GetHtmlDocument(levelOne.Url);
    if (doc == null)
        return lstResult;

    List<IHtmlElement> lists = doc.Find(".industries-header__section-lists").ToList()[1].Find("a").ToList();

    for (int i = 0; i < lists.Count; i++)
    {
        Industry item = new Industry();
        item.Name = lists[i].InnerHtml();
        item.Url = Url + lists[i].Attribute("href").AttributeValue;
        lstResult.Add(item);
    }
    return lstResult;
}

Step4. 定义存储股票信息的结构 StockCbs

public class StockCbs
{
    /// <summary>
    /// 股票名称
    /// </summary>
    public string StockName { get; set; }
    /// <summary>
    /// 股票编码
    /// </summary>
    public string StockId { get; set; }
    /// <summary>
    /// 网址
    /// </summary>
    public string Url { get; set; }
    /// <summary>
    /// 根据财报得出的评分
    /// </summary>
    public string CsbCourse { get; set; }
    /// <summary>
    /// 当前价格
    /// </summary>
    public string Price { get; set; }
    /// <summary>
    /// 市盈率
    /// </summary>
    public string Pe { get; set; }
    /// <summary>
    /// 市净率
    /// </summary>
    public string Pb { get; set; }
    /// <summary>
    /// 10倍市盈率
    /// </summary>
    public string Pe10 { get; set; }
    /// <summary>
    /// 20倍市盈率
    /// </summary>
    public string Pe20 { get; set; }
    /// <summary>
    /// 30倍市盈率
    /// </summary>
    public string Pe30 { get; set; }
}

Step5. 得到股票列表 List<StockCbs>

public static List<StockCbs> GetStocks(Industry industry)
{
    List<StockCbs> lstResult = new List<StockCbs>();
    IHtmlDocument doc = HtmlSpiter.GetHtmlDocument(industry.Url);
    if (doc == null)
        return lstResult;

    List<IHtmlElement> lists = doc.Find("table tbody tr").ToList();

    for (int i = 0; i < lists.Count; i++)
    {
        List<IHtmlElement> row = lists[i].Find("td").ToList();
        
        StockCbs item = new StockCbs();
        item.StockName = row[1].FindFirst(".company-link").InnerHtml().Trim();
        item.Url = Url + row[1].FindFirst(".company-link").Attribute("href").AttributeValue;
        item.StockId = row[1].FindFirst(".code-in-list").InnerHtml().Trim();
        item.CsbCourse = row[2].InnerHtml().Trim();

        doc = HtmlSpiter.GetHtmlDocument(item.Url);
        List<IHtmlElement> lst = doc.Find(".header-price").ToList();
        item.Price = lst[0].FindFirst(".value").InnerHtml().Trim();
        item.Pb = lst[0].Find(".pepb-ratio span").ToList()[1].InnerHtml().Trim();
        item.Pe = lst[0].Find(".pepb-ratio span").ToList()[0].InnerHtml().Trim();
        item.Pe10 = lst[0].FindFirst(".ratio-low").FindFirst(".data").InnerHtml().Trim();
        item.Pe20 = lst[0].FindFirst(".ratio-normal").FindFirst(".data").InnerHtml().Trim();
        item.Pe30 = lst[0].FindFirst(".ratio-high").FindFirst(".data").InnerHtml().Trim();

        lstResult.Add(item);
    }
    return lstResult;
}

Step6. 结果输出

  • 爬取一级行业:爬取数据,填充“一级行业下拉列表”。
  • 爬取二级行业:根据选择的一级行业,爬取数据,填充“二级行业下拉列表”。
  • 爬取网页:爬取二级行业对应的所有股票信息。
  • 跳转到网页:跳转到二级行业所在的网页。

结果输出


到此为止,关于如何利用 C# 语言爬取「财报说」中的股票数据,就介绍完了。大家有什么问题可以在图文的下方给我留言,今天就到这里吧!See You!


相关图文:

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青少年编程备考

感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值