C#三行代码获取优酷首页热门视频集合

Winzigege 2012-03-28 05:57:16
加精

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Youku
{
/// <summary>
/// 优酷热门视频实体类
/// </summary>
public class YouHotVideo
{
public string Title { get; set; }
public string ImgURI { get; set; }
public string URI { get; set; }

public YouHotVideo(string title, string imgUri, string uri)
{
Title = title;
ImgURI = imgUri;
URI = uri;
}

public static List<YouHotVideo> GetALL()
{
var v = new StreamReader(HttpWebRequest.Create("http://www.youku.com")
.GetResponse().GetResponseStream(), Encoding.UTF8).ReadToEnd()
.Replace("<li ", "❇").Split('❇').Where(x => x.Contains("v_link") || x.Contains("v_thumb"))
.Select(x => x.Substring(x.IndexOf("http"))).Select(x => x.Remove(x.IndexOf(">"))).ToList();

v = v.Select(x => x + v.ElementAt(v.IndexOf(x) + (v.IndexOf(x) == v.Count() - 1 ? 0 : 1))).ToList();

return v.Where(x => v.IndexOf(x) % 2 == 0).Where(x => x.Contains("html") && x.Contains("ykimg"))
.Select(x => x.Replace("<", "<").Replace(">", ">").Replace(""", "\"")
.Replace(" ", " ")).ToList().Select(x => new YouHotVideo(x.Remove(x.LastIndexOf("\""))
.Substring(x.IndexOf("title") + 7), x.Remove((x.Contains("src") ? x.LastIndexOf(" src") : x
.LastIndexOf(" alt")) - 1).Substring(x.IndexOf("ykimg") - 10), x.Remove(x.IndexOf("html") + 4))).ToList();
}
}
}


using System;

namespace ConsoleApplicationDemo
{
class Program
{
static void Main(string[] args)
{
Youku.YouHotVideo.GetALL().ForEach(x =>
{
Console.WriteLine("标题:" + x.Title);
Console.WriteLine("图片地址:" + x.ImgURI);
Console.WriteLine("视频地址:" + x.URI);
Console.WriteLine();
});
Console.ReadLine();
}
}
}

...全文
10473 240 打赏 收藏 转发到动态 举报
写回复
用AI写文章
240 条回复
切换为时间正序
请发表友善的回复…
发表回复
丶海口 2013-11-04
  • 打赏
  • 举报
回复
来个大家都看得懂的

	/// <summary>
	/// 优酷热门视频类
	/// </summary>
	public class YouHotVideo
	{
		public string Title { get; private set; }
		public string LogoURI { get; private set; }
		public string URI { get; private set; }

		public YouHotVideo(string title, string imgUri, string uri)
		{
			Title = title;
			LogoURI = imgUri;
			URI = uri;
		}

		public static List<YouHotVideo> GetALL()
		{
			List<YouHotVideo> HotVideos = new List<YouHotVideo>();

			var httpStream = HttpWebRequest.Create("http://www.youku.com").GetResponse().GetResponseStream();
			var content = new StreamReader(httpStream, Encoding.UTF8).ReadToEnd();

			Regex nodeRegex = new Regex("<div class=\"v-thumb\">");
			Regex imageRegex = new Regex("http://r3.ykimg.com/.{32}");
			Regex urlRegex = new Regex("http://v.youku.com/v_show/id_.*\\.html");
			Regex titleRegex = new Regex("title=\".*\"");

			var hotvideos = nodeRegex.Split(content);
			foreach (var hotvideo in hotvideos)
			{
				var title = WebUtility.HtmlDecode(titleRegex.Match(hotvideo).Value);
				var image = imageRegex.Match(hotvideo).Value;
				var url = urlRegex.Match(hotvideo).Value;
				HotVideos.Add(new YouHotVideo(title.Substring(7, title.Length - 8), image, url));
			}
			return HotVideos;
		}
	}
E次奥 2013-04-26
  • 打赏
  • 举报
回复
写了这么长!要是出个小错,都没法调试!
yujian199005 2013-04-26
  • 打赏
  • 举报
回复
启发了
ct4477xx 2012-04-22
  • 打赏
  • 举报
回复
都是高手……
Winzigege 2012-04-22
  • 打赏
  • 举报
回复
精简到了一行
[code=C#]
public static List<YouHotVideo> GetALL()
{
return new[]{ new WebClient { Encoding = Encoding.UTF8 }
.DownloadString("http://www.youku.com")
.Split(new[] { "<li " }, StringSplitOptions.RemoveEmptyEntries)
.Where(x => x.Contains("v_link") || x.Contains("v_thumb"))
.Select(x => x.Substring(x.IndexOf("http")))
.Select(x => x.Remove(x.IndexOf(">"))).ToList()}
.Select(v => v.Select(x => v.IndexOf(x) % 2 == 0 ? (x + v.ElementAt(v
.IndexOf(x) + 1)) : null).Where(x => x != null)
.Where(x => x.Contains("html") && x.Contains("ykimg"))
.Select(x => x.Replace("<", "<").Replace(">", ">")
.Replace(""", "\"").Replace(" ", " "))
.Select(x => new YouHotVideo(x.Remove(x.LastIndexOf("\""))
.Substring(x.IndexOf("title") + 7), x.Remove((x
.Contains("src") ?x.LastIndexOf(" src") : x
.LastIndexOf(" alt")) - 1).Substring(x.IndexOf("ykimg") - 10), x
.Remove(x.IndexOf("html") + 4)))
.ToList()).First();
}
Felven 2012-04-18
  • 打赏
  • 举报
回复
学习啊,最近要做这个
Winzigege 2012-04-11
  • 打赏
  • 举报
回复
[Quote=引用 222 楼 的回复:]

这种三行代码,效率就不说了,翻译成低级语言都不知道成多少行。用正则不超过5行。而且看起简单非常多。
[/Quote]
如果你对Linq和正则的原理有一点了解的话,你一定会为你所说的话而感到无地自容的。
尐孑 2012-04-11
  • 打赏
  • 举报
回复
标记一下
ly745455 2012-04-11
  • 打赏
  • 举报
回复
虽然看不懂但是那个return好像很牛逼的样子...


wangnadh 2012-04-11
  • 打赏
  • 举报
回复
感谢楼主,学习一下
Winzigege 2012-04-11
  • 打赏
  • 举报
回复
[Quote=引用 229 楼 的回复:]

代码写出来让人读的好不好!!!
有没人吧整个网站的脚本写在一句jQuery下?
[/Quote]
你根本就不懂Linq和Lambda你当然读不懂,代码写给懂的人看,到此为止吧,下面都别回复了。
大飞飞虫 2012-04-11
  • 打赏
  • 举报
回复
代码写出来让人读的好不好!!!
有没人吧整个网站的脚本写在一句jQuery下?
Alexander 2012-04-10
  • 打赏
  • 举报
回复
[Quote=引用 220 楼 的回复:]

这东西就像switch case语句一样,你完全可以用if else 达到同样的效果,但是某些时候你还是会用switch,至于为什么要用,你应该很清楚,因为你很熟悉这个语句的用法和目的,linq就是这样。。。他可以用其他东西,比如循环之类的达到同样的效果,但是你用不到只是因为你不像熟悉switch case一样熟悉Linq引用 219 楼 的回复:
Linq 适应性太低了啦 大项目基本上用不……
[/Quote]

linq初学者来学习下。感觉牛人好多。

PS:switch-case与if-else效果并不完全相同,switch-case实质上是一种空间换时间的实现,是通过一个地址映射表完成跳转的。也就是说相同逻辑的前提下,switch-case与if-else相比速度略高,内存占用略大。PC程序的设计通常优先考虑时间复杂度,所以建议使用switch-case结构(如果是嵌入式编程通常都在有限的存储器内工作,所以一般用if-else结构较好)。

虽然现在CPU都很快了,内存都很大了,很多人都认为没必要考虑效率的问题。但在下认为平时从细节上养成高效编码的习惯总比完全不当回事的好。
zscayz3 2012-04-10
  • 打赏
  • 举报
回复
真心鄙视那些说可读性差的人,往往根本不理解Where方法,Select方法甚至“=>”符号的意义,楼主没把代码规范分行你们就看不懂了,其实只是因为Where和Select所蕴含的信息量比你们想象的要多,每一次使用都像一个Sql语句一样。这些代码只是这两个方法和字符串处理方法的混合使用而已。我贴一个格式化的就好看了,代码没有改,只是规范一下缩进和分行而已

var v = new StreamReader(HttpWebRequest.Create("http://www.youku.com").GetResponse().GetResponseStream(), Encoding.UTF8)
.ReadToEnd()
.Replace("<li ", "❇").Split('❇')
.Where(x => x.Contains("v_link") || x.Contains("v_thumb"))
.Select(x => x.Substring(x.IndexOf("http")))
.Select(x => x.Remove(x.IndexOf(">"))).ToList();

v = v.Select(x => x + v.ElementAt(v.IndexOf(x) + (v.IndexOf(x) == v.Count() - 1 ? 0 : 1))).ToList();

return v.Where(x => v.IndexOf(x) % 2 == 0)
.Where(x => x.Contains("html") && x.Contains("ykimg"))
.Select(x => x.Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace(" ", " ")).ToList()
.Select(x =>new YouHotVideo(
x.Remove(x.LastIndexOf("\"")).Substring(x.IndexOf("title") + 7),
x.Remove((x.Contains("src") ? x.LastIndexOf(" src") : x.LastIndexOf(" alt")) - 1).Substring(x.IndexOf("ykimg") - 10),
x.Remove(x.IndexOf("html") + 4)
)
).ToList();
小茶杯 2012-04-10
  • 打赏
  • 举报
回复
LZ不错的。。支持、、、、、、
mfkuyg61 2012-04-10
  • 打赏
  • 举报
回复
不错,又学到了一点
  • 打赏
  • 举报
回复
来晚了,不过也收藏一下。。
一根葱的无奈 2012-04-10
  • 打赏
  • 举报
回复
世界太神奇了!
ck2022 2012-04-10
  • 打赏
  • 举报
回复
好的,谢谢分享
烈火蜓蜻 2012-04-10
  • 打赏
  • 举报
回复
这种三行代码,效率就不说了,翻译成低级语言都不知道成多少行。用正则不超过5行。而且看起简单非常多。
加载更多回复(166)

110,580

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧