110,580
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
}
/// <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;
}
}
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();