zoukankan      html  css  js  c++  java
  • 提取网页中的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();
    
                }
            }
        }
    }

    文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。 欢迎大家留言交流,转载请注明出处。
  • 相关阅读:
    常见jvm命令
    服务后台启动
    kafka创建topic,生产和消费指定topic消息
    kafka-manager安装
    修改ssh主机名
    设置虚拟机静态ip
    kafka术语
    cas和oauth2的区别
    会Python的大学生,步入职场将会非常抢手!
    python爬虫把url链接编码成gbk2312格式过程解析
  • 原文地址:https://www.cnblogs.com/yhlx125/p/2754155.html
Copyright © 2011-2022 走看看