如是题说.
获得图片的绝对地址.
获得图片的绝对地址.
解决方案 »
- 关于C#中null,"",empty的差别
- 关于System.Windows.Forms.ComboBox的Text属性的简单问题。
- datagridview 动态绑定数据源
- WebBrowser实现自动提交表单,如何提交radio和下拉框?
- 如何获取局域网内浏览者IP和机器名?
- 请问Windows 下如何读写创建 Sqlce的sdf文件
- repeater选择的行的取得数据问题
- 请问微软哪个认证适合做软件的人考?
- 关于反射事件的问题
- 怎样将两张表中相关数据绑定到DropDownList
- CS_Test2005_07_07, MS有没有带项目的例子,在MSDN什么地方呢?
- 网址转码http://www.csdn.net 转为 http%3a%2f%2fwww.csdn.net
{
if (!String.IsNullOrEmpty(strVal))
{
SetupItem SiteSet = SiteCaches.Setup;
Regex objRex = new Regex(@"((http|https|ftp):(\/\/|\\\\){1}(([A-Za-z0-9_-])+[.]){1,}(net|com|cn|org|cc|tv|de|us|[0-9]{1,3})(\S*\/)((\S)+[.]{1}(gif|jpg|jpeg|png)))", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
MatchCollection objMatchs = objRex.Matches(strVal);
String strFileDate = "Attach/month_" + DateTime.Now.ToString("yyMM");
String strFileFold = SiteGlobal.Locale + strFileDate;
String strFilePath = HttpContext.Current.Server.MapPath(strFileFold);
if (!Directory.Exists(strFilePath))
{
Directory.CreateDirectory(strFilePath);
}
String strFileName = String.Empty;
AttachItem objAttach;
HttpWebRequest objRequest;
HttpWebResponse objResponse;
Stream objStream;
FileStream objFile;
Int32 intCount = 1;
Uri strURL;
AttachList objAttachList = new AttachList();
foreach (Match objMat in objMatchs)
{
objAttach = new AttachItem();
objAttach.Author = SiteCaches.Guest;
objAttach.Address = objAttach.Author.Address;
strURL = new Uri(objMat.Groups[1].Value);
if (strURL.Host != new Uri(SiteSet.Address).Host)
{
objAttach.Prefix = Function.Random(6);
objAttach.Suffix = strURL.AbsoluteUri.Substring(strURL.AbsoluteUri.LastIndexOf('.') + 1);
objAttach.Publish = DateTime.Now;
objAttach.Filename = objAttach.Publish.ToString("HHmmss") + "_" + intCount.ToString();
strFileName = objAttach.Prefix + "_" + objAttach.Filename + "." + objAttach.Suffix;
try
{
objAttach.ID = Guid.NewGuid().ToString();
objRequest = (HttpWebRequest)WebRequest.Create(strURL);
objResponse = (HttpWebResponse)objRequest.GetResponse();
objStream = objResponse.GetResponseStream();
objAttach.Filesize = (Int32)objResponse.ContentLength;
if (objAttach.Filesize <= SiteSet.LimFile)
{
objAttach.Mimetype = objResponse.ContentType;
objFile = new FileStream(Path.Combine(strFilePath, strFileName), FileMode.Create, FileAccess.Write);
this.ReadWriteStream(objStream, objFile);
objAttachList.Add(objAttach);
this._OutResult = strVal.Replace(strURL.AbsoluteUri, SiteSet.Address + "/" + strFileDate + "/" + strFileName);
}
objStream.Close();
}
catch { }
intCount++;
}
}
BitracServices.InsertAttach(objAttachList);
}
}
private void ReadWriteStream(Stream rStream, Stream wStream)
{
Int32 intLength = 32768;
Byte[] objBuff = new Byte[intLength];
Int32 intByteRead = rStream.Read(objBuff, 0, intLength);
while (intByteRead > 0)
{
wStream.Write(objBuff, 0, intByteRead);
intByteRead = rStream.Read(objBuff, 0, intLength);
}
rStream.Close();
wStream.Close();
}
$2/ex:http://topic.csdn.net/u/20080202/09/5b4b090c-6727-4bb7-ad35-1b0bc8b46384.html$1_$2_$3Result:
http://topic.csdn.net_/u/20080202/09_/5b4b090c-6727-4bb7-ad35-1b0bc8b46384.html
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;namespace GrabStockSystem
{
class Grab
{
static void Main(string[] args)
{
//申明这个是最简单的,基本的,如果你要做获取的东西,数据量大的话,那么千万不要用webclient,这个超时不好处理
WebClient myweb = new WebClient();
myweb.Credentials = CredentialCache.DefaultCredentials; byte[] mybyte = myweb.DownloadData("http://csdn.net/");//传入网址
string mystring = Encoding.Default.GetString(mybyte);//得到源代码
Regex Re = new Regex(@"<img.*?src=(""|')?http(?<imageurl>[\s\S]*?)(""|'|\s)");//正则
MatchCollection Mc = Re.Matches(mystring);
foreach (Match M in Mc)
{
Console.WriteLine("http"+M.Groups["imageurl"].Value);
}
Console.ReadKey();
}
}
}
我是楼主
我是楼主
我是楼主
我是楼主那我只想找.jpg的字符呢??????????????????????????????????????????????????
这里无非两个难点
1,获得目标网页的html代码
2,组合自己的正则(要注意是最短匹配,呵呵,否则你很可能得到一个匹配项)
正则只能自己去根据规则写了,而获得目标网页的代码网上资料一堆(满足你的C#条件)
http:(.*?).jpg
RegexOptions.IgnoreCase);
foreach (Match mh in mXml)
{
CopyFile(strSub(strSourceHtm) + "\\" + mh.Value.ToString() + "xml", strTarget + "\\" + mh.Value.ToString() + "xml");
}
这个是取网页中xml的正则表达式.还有就是.当取img的时候.别忘了还有背景.backgroup