最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

.NET从优酷中采集所有视频及信息

日期:08-16    来源:中国设计秀    作者:cnwebshow.com

夜闻香原创,转载请保留此信息,万分感谢!oL3中国设计秀
博客: http://clso.cnblogs.com/oL3中国设计秀
主页: http://cleclso.cn/oL3中国设计秀
QQ:315514678 E-mail:clso#qq.comoL3中国设计秀
欢迎技术交流!oL3中国设计秀
oL3中国设计秀
oL3中国设计秀
因为想做一个视频点播类的网站,所以开始研究视频采集。oL3中国设计秀
oL3中国设计秀
这个方法就是提取优酷的专辑ID,然后一个个ID进行循环采集网页代码,从中提取title标签和VID,没什么技术含量。=..=oL3中国设计秀
oL3中国设计秀
采集中应用.NET中的HttpWebRequest和HttpWebResponse类,代码分析用了正则表达式。oL3中国设计秀
oL3中国设计秀
这个代码效率不是很好,一个网页的解析时间在0.5~2秒之间,不适合大量采集。也许将它转换成javaScript速度会快一点吧。oL3中国设计秀
oL3中国设计秀
暂时就研究这么多,代码直接发出来给大家共享一下。oL3中国设计秀
oL3中国设计秀
oL3中国设计秀
oL3中国设计秀
代码VB.NET,新建一个窗体frmMain,添加一个TextBox,一个ListBox,两个Button,复制下面的代码:oL3中国设计秀
oL3中国设计秀
oL3中国设计秀
oL3中国设计秀
Imports System.NetoL3中国设计秀
Imports System.IOoL3中国设计秀
Imports System.TextoL3中国设计秀
Imports System.Text.RegularExPRessionsoL3中国设计秀
oL3中国设计秀
Public Class frmMainoL3中国设计秀
oL3中国设计秀
    Structure VListoL3中国设计秀
        Dim id As IntegeroL3中国设计秀
        Dim title As StringoL3中国设计秀
        Dim vid1 As StringoL3中国设计秀
        Dim vid2 As StringoL3中国设计秀
oL3中国设计秀
        Overloads Function ToString() As StringoL3中国设计秀
            Return String.Format("{0}:<{1}> [{2}]", id, title, vid1)oL3中国设计秀
        End FunctionoL3中国设计秀
    End StructureoL3中国设计秀
oL3中国设计秀
    Dim myList As New List(Of VList)oL3中国设计秀
oL3中国设计秀
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickoL3中国设计秀
        ' 防止重复创建变量oL3中国设计秀
        Dim wr1 As HttpWebRequestoL3中国设计秀
        Dim wr2 As HttpWebResponseoL3中国设计秀
        Dim ret As StringoL3中国设计秀
        Dim reg As MatchoL3中国设计秀
        Dim g As GroupoL3中国设计秀
oL3中国设计秀
        Dim preVid As String = "" '上一个VIDoL3中国设计秀
        Dim nowid As Integer = 0 '当前的视频集数oL3中国设计秀
oL3中国设计秀
        Dim listUrl As String = TextBox1.Text '获取专辑URL,如 http://www.youku.com/playlist_show/id_2350764.htmloL3中国设计秀
        Dim tarUrl As String = "http://v.youku.com/v_playlist/f{0}" '{0}ListIDoL3中国设计秀
oL3中国设计秀
        reg = Regex.Match(listUrl, "playlist_show/id_(d+).*.html")oL3中国设计秀
        If Not reg.Success ThenoL3中国设计秀
            MsgBox("专辑列表提取失败!")oL3中国设计秀
            Exit SuboL3中国设计秀
        End IfoL3中国设计秀
        g = reg.Groups(1)oL3中国设计秀
        tarUrl = String.Format(tarUrl, g.Value) & "o{1}p{0}.html" '{0}集数 {1}排序oL3中国设计秀
oL3中国设计秀
        wr1 = HttpWebRequest.Create(TextBox1.Text)oL3中国设计秀
        wr2 = wr1.GetResponseoL3中国设计秀
        ret = New StreamReader(wr2.GetResponseStream, Encoding.GetEncoding(wr2.CharacterSet)).ReadToEndoL3中国设计秀
oL3中国设计秀
        reg = Regex.Match(ret, "<title>(.+) - 专辑 - 优酷视频</title>")oL3中国设计秀
        If Not reg.Success ThenoL3中国设计秀
            MsgBox("专辑名称提取失败!")oL3中国设计秀
        ElseoL3中国设计秀
            g = reg.Groups(1)oL3中国设计秀
            MsgBox("专辑名:《" & g.Value & "》")oL3中国设计秀
        End IfoL3中国设计秀
oL3中国设计秀
        DooL3中国设计秀
            ' 从Web流中获取页面文本oL3中国设计秀
            wr1 = HttpWebRequest.Create(String.Format(tarUrl, nowid, "0")) '按倒序方式查找视频oL3中国设计秀
            wr2 = wr1.GetResponseoL3中国设计秀
            ret = New StreamReader(wr2.GetResponseStream, Encoding.GetEncoding(wr2.CharacterSet)).ReadToEndoL3中国设计秀
oL3中国设计秀
            'TextBox2.Text = retoL3中国设计秀
oL3中国设计秀
            ' 创建一个临时视频列表变量oL3中国设计秀
            Dim nlist As New VListoL3中国设计秀
            nlist.id = nowid '获取IDoL3中国设计秀
            ' 获取videoIdoL3中国设计秀
            reg = Regex.Match(ret, "vars+videoIds*=s*""(d+)""s*;")oL3中国设计秀
            If Not reg.Success Then Exit DooL3中国设计秀
            g = reg.Groups(1)oL3中国设计秀
            ' 如果VID等于上一个VID最退出oL3中国设计秀
            If g.Value = preVid Then Exit DooL3中国设计秀
            nlist.vid1 = g.ValueoL3中国设计秀
            ' 获取videoId2oL3中国设计秀
            reg = Regex.Match(ret, "vars+videoId2s*=s*""((w|=)+)""s*;")  '"vars+videoId2s*=s*""(w+)""s*;")oL3中国设计秀
            If Not reg.Success Then Exit DooL3中国设计秀
            g = reg.Groups(1)oL3中国设计秀
            nlist.vid2 = g.ValueoL3中国设计秀
            ' 获取标题oL3中国设计秀
            reg = Regex.Match(ret, "<title>(.+) - (.+) - 视频 - 优酷视频 - 在线观看 - </title>")oL3中国设计秀
            If Not reg.Success ThenoL3中国设计秀
                nlist.title = "{名称查找错误}"oL3中国设计秀
            ElseoL3中国设计秀
                g = reg.Groups(2)oL3中国设计秀
                nlist.title = g.ValueoL3中国设计秀
            End IfoL3中国设计秀
            ' 收尾工作oL3中国设计秀
            myList.Add(nlist) '添加到总列表中oL3中国设计秀
            preVid = nlist.vid1 '记录最后一个VIDoL3中国设计秀
            wr2.Close()oL3中国设计秀
oL3中国设计秀
            Me.Text = nowid & " : 处理完成!"oL3中国设计秀
oL3中国设计秀
            nowid += 1oL3中国设计秀
oL3中国设计秀
        LoopoL3中国设计秀
oL3中国设计秀
        wr2.Close()oL3中国设计秀
        MsgBox(nowid & " 个视频全部采集处理完成!")oL3中国设计秀
oL3中国设计秀
        Button2_Click(sender, e)oL3中国设计秀
    End SuboL3中国设计秀
oL3中国设计秀
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ClickoL3中国设计秀
        ListBox1.Items.Clear()oL3中国设计秀
        For Each ls As VList In myListoL3中国设计秀
            ListBox1.Items.Add(String.Format("{0}:<{1}> [{2}]", ls.id, ls.title, ls.vid1))oL3中国设计秀
        NextoL3中国设计秀
        myList.Clear()oL3中国设计秀
    End SuboL3中国设计秀
oL3中国设计秀
oL3中国设计秀
End ClassoL3中国设计秀

本文引用地址:/bc/article_46150.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明