获取网页中全部图片

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using ;

using System.IO;

using System.Text;

using System.Text.RegularExpressions;

namespace Syccw.Controllers

{

public class Get_Url

{

///

///获取网页中全部图片

///

///网页地址

///网页编码,为空自动判断

///全部图片显示代码

public string getImages(string url,string charSet)

{

string s = getHtml(url, charSet);

return getPictures(s, url);

}

///

///获取网页中全部图片

///

///网址

///全部图片代码

public string getImages(string url)

{

return getImages(url, "");

}

public string doman(string url)

{

Uri u = new Uri(url);

return u.Host;

}

///

///获取网页内容

///

///网站地址

///目标网页的编码,如果传入的是null或者"",那就自动分析网页的编码

///

public string getHtml(string url, string charSet)

{

WebClient myWebClient = new WebClient();

//创建WebClient实例myWebClient

// 需要注意的:

//有的网页可能下不下来,有种种原因比如需要cookie,编码问题等等

//这是就要具体问题具体分析比如在头部加入cookie

// webclient.Headers.Add("Cookie", cookie);

//这样可能需要一些重载方法。根据需要写就可以了

//获取或设置用于对向Internet 资源的请求进行身份验证的网络凭据。

myWebClient.Credentials = CredentialCache.DefaultCredentials;

//如果服务器要验证用户名,密码

//NetworkCredential mycred = new NetworkCredential(struser, strpassword);

//myWebClient.Credentials = mycred;

//从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)

byte[] myDataBuffer = myWebClient.DownloadData(url);

string strWebData = Encoding.Default.GetString(myDataBuffer);

//获取网页字符编码描述信息

Match charSetMatch = Regex.Match(strWebData, "

string webCharSet = charSetMatch.Groups[2].Value.Replace("\"", "");

if (charSet == null || charSet == "")

charSet = webCharSet;

if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default) strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);

return strWebData;

}

public string getPictures(string data, string url)

{

MatchCollection ps = Regex.Matches(data,

@"]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>");

string s = string.Empty;

for (int i = 0; i < ps.Count; i++)

{

pictures p = new pictures(ps[i].Value, url);

s += p.GetHtml + "
" + Environment.NewLine;

}

return s;

}

///

///图片文件属性处理类

///

public class pictures

{

public pictures(string strHtml,string baseUrl)

{

_html = strHtml;

Uri u1 = new Uri(baseUrl);

_doman = u1.Host;

_baseUrl = u1.Scheme + "://" + _doman;

setSrc();

}

private string _html = string.Empty;

private string _baseUrl = string.Empty;

private string _doman = string.Empty;

public string GetHtml

{

get { return _html; }

}

相关文档
最新文档