使用正则找出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
{
//
}
}
}
}
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
{
//
}
}
}
}