提取网页中的javascript脚本和下载链接

  网上有个不错的视频教程,是swf格式的,想下载下来,但是网页太多了,每次打开网页查看源码再定位到那么JavaScript块,复制粘贴,好不繁琐。于是就想通过程序来减少工作量。

程序功能:批量提取网页中的Javascript脚本,提取脚本中的视频下载链接信息。

首先通过网络请求网页,得到响应的流文件,通过正则表达式匹配提取其中的JavaScript脚本块。再匹配提出Url下载链接。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace CsWebBrower
{
    public partial class GetUri : Form
    {
        public GetUri()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] strs = textBox1.Lines;
            foreach (string str in strs)
            {
                Uri uri = new Uri(str);


                WebRequest req = WebRequest.Create(uri);

                WebResponse result = req.GetResponse();
                Stream ReceiveStream = result.GetResponseStream();
                StreamReader readerOfStream = new StreamReader(ReceiveStream,
                    System.Text.Encoding.GetEncoding("UTF-8"));
                string temp = readerOfStream.ReadToEnd();
                //Regex ex = new Regex("<script.+?type ?= ?(/\"|')text/javascript(/\"|')>.*?</script>",
                //RegexOptions.Singleline);
                MatchCollection mc = Regex.Matches(temp, @"<script[^>]*>[\s\S]*?</script>", RegexOptions.IgnoreCase);
                foreach (Match m in mc)
                {

                    //MatchCollection mc2 = Regex.Matches(m.Value, @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.IgnoreCase);[a-zA-z]+://[^\s]*
                    MatchCollection mc2 = Regex.Matches(m.Value, @"http://[\s\S]*?.swf", RegexOptions.IgnoreCase);
                    foreach (Match m2 in mc2)
                    {
                        richTextBox1.Text += m2.Value + "\n\n";
                    }

                }
                readerOfStream.Close();
                ReceiveStream.Close();

            }
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值