zoukankan      html  css  js  c++  java
  • 爬虫

    获得榜单的前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();
            }
        }
    }
  • 相关阅读:
    常用正则表达式
    The Skins Factory 界面设计欣赏
    The Regulator 轻松上手
    Visual C#的Web XML编程
    业务流程不是需求
    如使用ODBC连接informix
    AJAX在信息系统中的应用研究
    浅谈几个SQL的日志概念
    量产 朗科(Netac)朗盛系列闪存盘E108 8G 手记
    【转】告诉大家他们是怎么成为富翁的
  • 原文地址:https://www.cnblogs.com/tqlin/p/5222602.html
Copyright © 2011-2022 走看看