使用正则找出CSS中的图片并下载下来

使用正则找出CSS中的图片并下载下来

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.Threading;
using  System.Text.RegularExpressions;

namespace  Win.Regx
{
    
public   partial   class  FrmUrl : Form
    {
        
public  FrmUrl()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls 
=   false ;
        }

        
private   void  FrmUrl_Load( object  sender, EventArgs e)
        {

        }

        
private   void  btn_Click( object  sender, EventArgs e)
        {
            Thread th 
=   new  Thread( new  ThreadStart(GetData));
            th.Start();
        }

        
///   <summary>
        
///  读取文件,整个匹配
        
///   </summary>
         private   void  GetData()
        {
            
string  dir  =  Application.StartupPath  +   " \\ " ;
            
string  file  =  dir  +   " aio0806.css " ;
            StringBuilder sb 
=   new  StringBuilder();
            
if  (File.Exists(file))
            {
                StreamReader tr 
=   new  StreamReader(file, Encoding.Default);
                
string  content  =  tr.ReadToEnd();
                tr.Close();

                
int  i  =   0 ;
                
//  方法一:
                
//  Regex regex = new Regex("url\\(['\"]?\\s*[^>]+?['\"]?\\s*\\)", RegexOptions.IgnoreCase);
                
//  方法二:
                Regex regex  =   new  Regex( " url\\(['\ " ] ? \\s * ( ?< url > [ ^> ] +? )[ ' \"]?\\s*\\)", RegexOptions.IgnoreCase);
                MatchCollection matches  =  regex.Matches(content);

                
int  lastIndex  =   0 ;
                
foreach  (Match match  in  matches)
                {
                    
if  (match.Index  >  lastIndex)
                    {
                        sb.Append(content.Substring(lastIndex, match.Index 
-  lastIndex));
                    }
                    i
++ ;
                    lbl.Text 
=   string .Format( " 总数:{0} " , i);
                    Group g 
=  match.Groups[ " url " ];
                    Capture c 
=  g.Captures[ 0 ];

                    txt.AppendText(c.Value 
+   " \r\n " );
                    
//  下载文件
                    
// DownFile(c.Value);

                    sb.Append(
string .Format( " url(images/{0}) " , Path.GetFileName(c.Value)));

                    lastIndex 
=  match.Index  +  match.Length;
                }
                
if  (lastIndex  <  content.Length)
                {
                    sb.Append(content.Substring(lastIndex, content.Length 
-  lastIndex));
                }
            }
            txt.AppendText(sb.ToString());
            txt.AppendText(
" \r\n\r\n========END======== " );
        }

        
///   <summary>
        
///  读取文件,单行匹配
        
///   </summary>
         private   void  GetData1()
        {
            
string  dir  =  Application.StartupPath  +   " \\ " ;
            
string  file  =  dir  +   " aio0806.css " ;
            
if  (File.Exists(file))
            {
                StreamReader tr 
=   new  StreamReader(file, Encoding.Default);
                
string  line  =  tr.ReadLine().Trim();
                StringBuilder sb 
=   new  StringBuilder();
                
int  i  =   0 ;
                
while  (line  !=   null   &&   ! tr.EndOfStream)
                {
                    
//  分解出所有的图片文件
                    
// Match m = Regex.Match(line, "url((\\s+['\"]?[^>]+?['\"]?\\s+))$");
                    Match m  =  Regex.Match(line,  " http://([^>]+?)(.gif|.jpg) " , RegexOptions.IgnoreCase);
                    
while  (m.Success)
                    {
                        i
++ ;
                        lbl.Text 
=   string .Format( " 总数:{0} " , i);
                        
string  url  =  m.Groups[ 0 ].ToString();
                        txt.AppendText(
string .Format( " {0}\r\n " , url));
                        DownFile(url);
                        m 
=  m.NextMatch();
                    }

                    line 
=  tr.ReadLine().Trim();
                }
                sb.Length 
=   0 ;

                tr.Close();
                txt.AppendText(
" ========END======== " );
            }
        }

        
///   <summary>
        
///  下载文件
        
///   </summary>
        
///   <param name="url"></param>
         private   void  DownFile( string  url)
        {
            
try
            {
                
string  dir  =   string .Format( " {0}\\images\\ " , Application.StartupPath);
                
if  ( ! Directory.Exists(dir)) Directory.CreateDirectory(dir);
                
string  fileName  =   string .Format( " {0}{1} " , dir, Path.GetFileName(url));
                WebRequest req 
=  WebRequest.Create(url);
                WebResponse res 
=  req.GetResponse();
                Stream stream 
=  res.GetResponseStream();
                FileStream fs 
=   new  FileStream(fileName, FileMode.OpenOrCreate);
                
int  l;
                
byte [] buffer  =   new   byte [ 1024 ];
                
do
                {
                    l 
=  stream.Read(buffer,  0 , buffer.Length);
                    
if  (l  >   0 )
                        fs.Write(buffer, 
0 , l);

                } 
while  (l  >   0 );

                fs.Close();
                fs.Flush();
                stream.Close();
                stream.Flush();
            }
            
catch
            {
                
//
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/angushine/archive/2008/09/08/1286964.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值