using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms; namespace ImageCollection
{
public partial class Form1 : Form
{
private static string Path = AppDomain.CurrentDomain.BaseDirectory + "img";
public Form1()
{
InitializeComponent();
} private void btnshuaqu_Click(object sender, EventArgs e)
{
string url = txturl.Text.Trim();
if (string.IsNullOrEmpty(url))
{
MessageBox.Show("请输入URl");
return;
}
txtimg.AppendText("开始抓取中:\r\n");
Thread th = new Thread(() => ShuaQu(url)) { IsBackground = true };
th.Start();
} private void ShuaQu(string url)
{
DirectoryInfo di = new DirectoryInfo(Path);
if (System.IO.Directory.Exists(Path))
{
di.Delete(true);
}
System.IO.Directory.CreateDirectory(Path);
string result = WebHttp.HttpGet(url, null, );
string[] str = GetHtmlImageUrlList(result);
txtimg.Invoke(new Action(() =>
{
txtimg.AppendText("已经获取到数据!"+str.Count() + "\r\n");
}));
//建立获取网页标题正则表达式
String regex = @"<title>.+</title>"; //返回网页标题
String title = Regex.Match(result, regex).ToString();
txttitle.Invoke(new Action(() => {
txttitle.Text = Regex.Replace(title, @"[\""]+", "");
}));
foreach (string s in str)
{
Uri u = new Uri(s);
if (u.Host == "www.xxx.com")
{
Thread downimg = new Thread(() => Get_img(s)) { IsBackground = true };
downimg.Start();
txtimg.Invoke(new Action(() => {
txtimg.AppendText(s + "\r\n");
}));
}
}
txtimg.Invoke(new Action(() =>
{
txtimg.AppendText("全部抓取完成!\r\n");
}));
} public void Get_img(string imgpath)
{ string[] file = imgpath.Split('?');
string name = System.IO.Path.GetFileName(file[]);
WebClient mywebclient = new WebClient();
mywebclient.DownloadFile(imgpath, Path + @"\" + name);
//Bitmap img = null;
//HttpWebRequest req;
//HttpWebResponse res = null;
//try
//{
// System.Uri httpUrl = new System.Uri(imgpath);
// req = (HttpWebRequest)(WebRequest.Create(httpUrl));
// req.Timeout = 180000; //设置超时值10秒
// req.UserAgent = "XXXXX";
// req.Accept = "XXXXXX";
// req.Method = "GET";
// res = (HttpWebResponse)(req.GetResponse());
// img = new Bitmap(res.GetResponseStream());//获取图片流
// img.Save(Path + @"\"+name);//随机名
//} //catch (Exception ex)
//{
// string aa = ex.Message;
//}
//finally
//{
// res.Close();
//}
} /// <summary>
/// 取得HTML中所有图片的 URL。
/// </summary>
/// <param name="sHtmlText">HTML代码</param>
/// <returns>图片的URL列表</returns>
private string[] GetHtmlImageUrlList(string sHtmlText)
{
// 定义正则表达式用来匹配 img 标签
Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); // 搜索匹配的字符串
MatchCollection matches = regImg.Matches(sHtmlText);
int i = ;
string[] sUrlList = new string[matches.Count]; // 取得匹配项列表
foreach (Match match in matches)
sUrlList[i++] = match.Groups["imgUrl"].Value;
return sUrlList;
}
}
}
#region 下载图片到Image
public static Image UrlToImage(string url) {
WebClient mywebclient = new WebClient();
byte[] Bytes = mywebclient.DownloadData(url);
using (MemoryStream ms = new MemoryStream(Bytes)) {
Image outputImg = Image.FromStream(ms);
return outputImg;
}
}
#endregion

C# 抓取网页的img src带参数的图片链接,并下载的更多相关文章

  1. 使用wget工具抓取网页和图片 成功尝试

    使用wget工具抓取网页和图片 发表于1年前(2014-12-17 11:29)   阅读(2471) | 评论(14) 85人收藏此文章, 我要收藏 赞7 wget 网页抓取 图片抓取 目录[-] ...

  2. 使用wget工具抓取网页和图片 及 相关工具几个

    想保存一些网页,最后找到这 wget 的 shell脚本,虽然不是太理想,亲测可用呢. 使用wget工具抓取网页和图片   来源 https://my.oschina.net/freestyletim ...

  3. Java 抓取网页中的内容【持续更新】

    背景:前几天复习Java的时候看到URL类,当时就想写个小程序试试,迫于考试没有动手,今天写了下,感觉还不错 内容1. 抓取网页中的URL 知识点:Java URL+ 正则表达式 import jav ...

  4. C语言调用curl库抓取网页图片

    思路是先用curl抓取网页源码,然后以关键字寻找出图片网址.   #include <stdio.h> #include <stdlib.h> #include <str ...

  5. C语言调用curl库抓取网页图片(转)

    思路是先用curl抓取网页源码,然后以关键字寻找出图片网址.  范例: #include <stdio.h> #include <stdlib.h> #include < ...

  6. python分布式抓取网页

    呵呵,前两节好像和python没多大关系..这节完全是贴代码, 这是我第一次写python,很多地方比较乱,主要就看看逻辑流程吧. 对于编码格式确实搞得我头大..取下来页面不知道是什么编码,所以先找c ...

  7. delphi 7中使用idhttp抓取网页 解决假死现象

    在delphi 7中使用idhttp抓取网页,造成窗口无反应的假死状态.通过搜索获得两种方法. 1.写在线程中,但是调用比较麻烦 2.使用delphi 提供的idantifreeze(必须安装indy ...

  8. PHP抓取网页图片

    <?php set_time_limit(0);//抓取不受时间限制 if($_POST['Submit']=="开始抓取"){ $URL=$_POST['link']; g ...

  9. HttpClient(一)HttpClient抓取网页基本信息

    一.HttpClient简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包, 并且它支 ...

随机推荐

  1. 使用MSYS、Notepad++搭建C/C++开发环境

    目标说明 本文的目标是教会大家如何用Notepad++来编写C/C++代码,并能够编译运行. 注:Notepad++是一个非常优秀的开源文本编辑器.官网地址 http://notepad-plus-p ...

  2. libevent源码分析:evmap_io_active_函数

    evmap_io_active_函数用于将激活指定文件描述符上的事件 void evmap_io_active_(struct event_base *base, evutil_socket_t fd ...

  3. eclipse jetty 请求的操作无法在使用用户映射区域打开的文件上执行

    使用jetty  debug代码启动  在eclipse中进行运行调试,碰到无法编辑保存webapp下的文件 提示: 请求的操作无法在使用用户映射区域打开的文件上执行 解决方法: 从 jetty 7 ...

  4. linux find 命令查找文件和文件夹

    查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) -name 查找关键字 -print 详解: find命令用来在指定目录下查找文件.任 ...

  5. 慢慢来,比较快。四年Android,共勉。

    四年阿,我特么以为我才17岁,不想已经27了,操蛋阿.加班熬夜,我的黑眼圈,黑头泛滥.如果木有自拍神器,我是决计不会爆照的!! 第一年,学了七天android找上了第一份工作,后来接了个外包,是做智能 ...

  6. win7下memCache安装过程

    1.下载memcache 的windows 稳定版,解压放某个盘下面,比如在H:/wamp/www/php api/memcache: 2.在终端(即cmd 命令界面)下,输入安装命令 :H:/wam ...

  7. # Writing your-first Django-app-part 5 -test

    确认bug 写test测试暴露bug 修复bug 更多测试例子 测试一个view The Django test client测试客户端. 提升DemoAppPoll/views.py 测试我们的vi ...

  8. [Timer]应用层实现sleep

    转自:https://www.cnblogs.com/longbiao831/p/4556246.html Select只能做延时,可以做回调吗? 本文讲述如何使用select实现超级时钟.使用sel ...

  9. js 日期格式化函数

    直接上代码: // 日期格式化函数 // yyyy/MM/dd hh:mm:ss SSS ⇒ "2017/05/16 09:24:20 850" //"yyyy/M/d ...

  10. mysql innodb_buffer_pool_size mysql占用内存大小和主从复制并行线程数量

    innodb_buffer_pool_size   set global slave_parallel_workers=4;