使用HtmlAgilityPack抓取Ethereum Tokens信息

使用HtmlAgilityPack抓取Ethereum Tokens信息

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                for (int i = 1; i <= 11; i++)
                {
                    string url = "https://etherscan.io/tokens?p="+i;
                    HtmlWeb webClient = new HtmlWeb();
                    HtmlDocument doc = webClient.Load(url);

                    var tbody = doc.DocumentNode.SelectSingleNode("//*[@id='ContentPlaceHolder1_divresult']/table/tbody");
                    var trItems = tbody.SelectNodes("tr");
                    foreach (var tr in trItems)
                    {
                        try
                        {
                            var tdItems = tr.SelectNodes("td");
                            string id = tdItems[0].SelectSingleNode("b//span").InnerHtml.Replace(" ", "");
                            string contractAddress = tdItems[1].SelectSingleNode("a").Attributes["href"].Value.Replace("/token/", "");
                            string tokenLogo = "https://etherscan.io" + tdItems[1].SelectSingleNode("a/img").Attributes["src"].Value;
                            string temp = tdItems[2].SelectSingleNode("h5/a").InnerHtml;
                            string tokenName = temp.Substring(0, temp.IndexOf(" "));
                            string tokenSymbol = temp.Substring(temp.IndexOf("(") + 1, temp.IndexOf(")") - temp.IndexOf("(") - 1);
                            string tokenDescribe = tdItems[2].SelectSingleNode("small/font").InnerHtml;

                            string tokenUrl = "https://etherscan.io/token/" + contractAddress;
                            HtmlWeb webtokenClient = new HtmlWeb();
                            HtmlDocument tokendoc = webtokenClient.Load(tokenUrl);
                            string tokenDecimal = tokendoc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/div[5]/div[1]/div[2]/table[1]/tr[2]/td[2]").InnerHtml.Replace("\n", "");
                            Console.WriteLine($"{id}\t{contractAddress}\t{tokenSymbol}\t{tokenDecimal}\t{tokenName}\t{tokenLogo}\t{tokenDescribe}\t");
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.Read();
        }
    }

posted @ 2018-07-02 14:40  小何同學  阅读(406)  评论(0编辑  收藏  举报