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();
            }
        }
    }
  • 相关阅读:
    C#.NET Winform 快速开发平台
    .Net C/S系统开发框架(楚楚原创)
    C# Winform 开发框架
    php导出excel表格超链接
    tp3使用PHPExcel 导出excel
    tp文件上传、表格转数组
    BUG修复记录
    tp3切库问题记录
    个人总结
    初识爬虫(番外篇-python)
  • 原文地址:https://www.cnblogs.com/tqlin/p/5222602.html
Copyright © 2011-2022 走看看