比如说
我要采集百度所有美女图片要如何采集还有采集图片的正则表达式要如何写!!请赐教!!
我要采集百度所有美女图片要如何采集还有采集图片的正则表达式要如何写!!请赐教!!
解决方案 »
- gridview中的cells(名称)能指定名称吗?
- 请教下一个日历控件问题
- vs 自带控件问题
- 100分散:再分享一个ASP.NET长文章分页显示函数,先到先得分
- MSDN站点里得这个效果怎么做?
- 我做了一个留言本..向数据库提交数据的时候就会提示"myCommand.ExecuteNonQuery()"这行有错误...前高手指点`~~`
- 关于dropdownlist控件出错的可能,有哪为高手能告知
- 希望在textboxchanged时候,触发后台代码,还触发JS的代码
- 在try...catch中捕捉异常,将错误号做为参数传入类库函数except(try...catch外抛出异常)集中处理,在函数中根据错误号显示在web.config中的
- 如何在ASP.NET中用程序代码设定DATAGRID每列的宽度以及折不折行
- 在线等,解决后结贴
- 网页打开太慢怎么解决?
{
List<string> list = new List<string>();
string temp = string.Empty;
string htmlStr = getHtml(url);
MatchCollection matchs = Regex.Matches(htmlStr, @"<(IMG|img)[^>]+>"); //抽取所有图片
for (int i = 0; i < matchs.Count; i++)
{
list.Add(matchs[i].Value);
}
return list;
} //所有图片路径(如果是相对路径的话,自动设置成绝对路径)
public static List<string> getImgPath(string url)
{
List<string> list = new List<string>();
string htmlStr = getHtml(url);
string pat = @"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>";
MatchCollection matches = Regex.Matches(htmlStr, pat, RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match m in matches)
{
string imgPath = m.Groups["imgUrl"].Value.Trim();
if (Regex.IsMatch(imgPath, @"\w+\.(gif|jpg|bmp|png)$")) //用了2次匹配,去除链接是网页的 只留图片
{
if (!imgPath.Contains("http"))//必须包含http 否则无法下载
{
imgPath = getUrl(url) + imgPath;
}
list.Add(imgPath);
}
}
return list;
} //下载图片
public void DownloadImg(string fileurl)
{
if (fileurl.Contains('.'))//url路径必须是绝对路径 例如http://xxx.com/img/logo.jpg
{
string imgName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + fileurl.Substring(fileurl.LastIndexOf('.')); // 生成图片的名字
string filepath = System.Web.HttpContext.Current.Server.MapPath("") + "/" + imgName;
WebClient mywebclient = new WebClient();
mywebclient.DownloadFile(fileurl, filepath);
}
}
//少复制了一个方法
public static string getHtml(string url)
{
try
{
string str = "";
Encoding en = Encoding.GetEncoding(getEncoding(url));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Set("Pragma", "no-cache");
request.Timeout = 30000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
{
Stream strM = response.GetResponseStream();
StreamReader sr = new StreamReader(strM, en);
str = sr.ReadToEnd();
strM.Close();
sr.Close();
}
return str;
}
catch
{
return String.Empty;
}
}
哥,是不是还少一个getEncoding的方法!!