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. svn 版本管理与自动部分发布

    作为团队开发项目时,会遇到项目的版本管理,测试部署与发布部署,下面是摘至他人的关于版本管理和自动部署的方案. svn自动部署的实现: 使用svn的hook功能 1.在版本库的hooks目录下面,有一些 ...

  2. 大牛总结的Linux提权Exp合集

    https://github.com/SecWiki/linux-kernel-exploits #CVE #Description #Kernels CVE-2017-1000367 [Sudo] ...

  3. Android Toolbar的使用 顶部标题栏+后退键

    最近设计安卓里面有个标题栏,里面有个后退键,可以完成后退之类的功能. 好,刚好可以用Toolbar去实现 上代码:activity_main.xml <?xml version="1. ...

  4. 【WPF】代码触发Button点击事件

    先定义Button按钮并绑定事件. public void test() { Button btn = new Button(); btn.Click += Btn_Click; } private ...

  5. 【Unity】自定义编辑器窗口——拓展编辑器功能

    最近学习了Unity自定义编辑器窗口,下面简单总结,方便用到时回顾. 新建一个脚本: using UnityEngine; using System.Collections; using UnityE ...

  6. B2C和B2B之间有多大差距

    从产品应用的角度,我们团队经历了企图将B2C系统套用到B2B业务流程上的阶段,对于自营业务这还勉强可以实施,但对于外部用户的实施难度就太大了,用户体验也不好.这个过程中,我只关注了技术范畴的迭代速度. ...

  7. Android——shape和selector和layer-list的(详细说明 转)

    <shape>和<selector>在Android UI设计中经常用到.比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到<shape> ...

  8. LVS的优点和缺点

    LVS的优点是:1.抗负载能力强.是工作在网络4层之上仅作分发之用,没有流量的产生,这个特点也决定了它在负载均衡软件里的性能最强的,对内存和cpu资源消耗比较低.2.配置性比较低,这是一个缺点也是一个 ...

  9. require.js+knockout.js+.underscore模板引擎的使用

    第一种使用方式: HTML: <ul data-bind="template: { name: 'peopleList' }"></ul> <scri ...

  10. oozie无法识别hadoopHA中的ns1

    [hadoop@dwdev-name1 m_goods_sale_detail]$ oozie job -config job.properties -run Error: E1603 : java. ...