• 爬虫


    获得榜单的前166部电影的评分总和(http://movie.douban.com/top250)

    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            public static string GetUrlRequerstInfo(string url)
            {
                string strBuff = "";
                Uri httpURL = new Uri(url);
                HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
                HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
                Stream respStream = httpResp.GetResponseStream();
                StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8);
                strBuff = respStreamReader.ReadToEnd();
                return strBuff;
            }  
    
            static void Main(string[] args)
            {
                int len = 250 / 25;
                string result = "";
                int start = 0;
                string regex2 = "<span class="rating_num" property="v:average">.+</span>";
    
                Regex re = new Regex(regex2);
    
                int num = 0;//总分
                bool isEnd = false;
    
                decimal total = 0;
                for (int i = 0; i < len; i++)
                {
                    if (!isEnd)
                    {
                        start = i * 25;
                        result = GetUrlRequerstInfo("http://movie.douban.com/top250?start=" + start + "&filter=");
    
                        MatchCollection matches = re.Matches(result);
                        System.Collections.IEnumerator enu = matches.GetEnumerator();
                        while (enu.MoveNext() && enu.Current != null)
                        {
                            Match match = (Match)(enu.Current);
                            string s = match.Value;
                            try
                            {
                                total += Convert.ToDecimal(s.Replace("<span class="rating_num" property="v:average">", "").Replace("</span>", ""));
                                num++;
                                if (num == 166)
                                {
                                    isEnd = true;
                                    break;
                                }
                            }
                            catch (Exception)
                            {
    
                            }
                        }
                    }
                }
                Console.WriteLine(total);
                Console.Read();
            }
        }
    }
  • 相关阅读:
    删除:恶意主页
    Winuser.h
    安天磁盘免疫工具研究的初步解答
    C#读写XML文件
    阻止系统关机
    在WebBrowser中屏蔽对话框
    如何用正确的方法写出高质量软件的75条体会
    怪事~
    GRUB4DOS中文自述文档;Grub4dos中文ReadMe
    开始菜单变成的经典样式,XPsuaa样式丢失
  • 原文地址:https://www.cnblogs.com/tqlin/p/5222602.html
Copyright © 2020-2023  润新知