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. JIRA python篇之展示多人未完成任务列表

    [本文出自天外归云的博客园] 通过python中的jira类我们可以方便的操作jira,获取一些我们想要再加工的信息. 这里举例,用html页面的形式展示分派给组内每个人的任务(未完成的.正在进行中的 ...

  2. sqlserver 目录名称无效解决办法

    问题描述: 1.sqlserver 打开表提示:目录名称无效 2.在执行sql语句时提示:在执行批处理时出现错误.错误消息为: 目录名无效 3.所有的数据库都存在1跟2的问题 问题分析: 1.操作系统 ...

  3. /.well-known/apple-app-site-association

    Technical Q&A QA1919 Incoming requests for /.well-known/apple-app-site-association file Q:  Why ...

  4. ElasticSearch的安装、使用、踩坑

    最近博客写的少了. 本篇介绍在安装ElasticSearch和head插件的过程中遇到的小问题,和一些日常使用的操作(简单搜索语法.分片管理). ElasticSearch 它是一个实时分布式搜索和分 ...

  5. Linxu磁盘分区

    http://vbird.dic.ksu.edu.tw/linux_basic/0130designlinux.php#hardware_know(好文章) http://blog.chinaunix ...

  6. Android Bitmap 缩放 旋转 水印 裁剪操作

    在android当中,Bitmap代表一个图片,里面封装了图片相关的信息. 一.将图片进行缩放操作 1.获得Bitmap对象 Bitmap bitmap = BitmapFactory.decodeR ...

  7. Go Revel - Websockets

    revel提供了对`Websockets`的支持. 处理`Websockets`链接: 1.添加一个`WS`类型方法的路由 2.添加一个action接受 `*websocket.Conn`参数 例如, ...

  8. NHibernate 知识点整理

    1.实体bool类型属性与数据库映射.如实体中有bool属性IsDelete.配置如下 <property column="IsDelete" name="IsDe ...

  9. flume1.8实现hdfsSink整点滚动文件

    由于官方的1.8版本hdfs-sink不能在每天的0点滚动文件,所以修改了flume-hdfs-sink源码. flume-hdfs-sink中修改了HDFSEventSink.java文件,其他文件 ...

  10. Shuffle机制

    一个map task处理一个切片Split,切片是一个范围的数据,和blocksize大小没有必然关系. 1.每个map有一个环形内存缓冲区,用于存储任务的输出.默认大小100MB(io.sort.m ...