如果你是做自媒体行业的,肯定要找素材,包括文字、图片和视频等。今天就介绍一个快速获取图片的方法,可以说是通用的,但需要一点编程技巧。
随便打开一个你要获取图片的页面,我们以这篇为例,在浏览器里打开,按F12键,出现源码面板。
找到上面这个像光标的按钮,点一下,再把鼠标移动图片上,下面的代码就能看到图片的src地址,这个地址就是图片的下载路径。如果想批量下载这篇博客里的所有图片,就把往上找,找到能包含所有科片的那个html结点。右键:Copy->Copy outerHTML,这样我们就拿到了包含所有图片的html源码。
批量获取图片,我们使用HtmlAgilityPack组件,完整代码如下
//解析新浪微博视频
paramText = this.fmParamText.Text.Trim();
if (string.IsNullOrEmpty(paramText)) return;
List<string> listP = new List<string>();
string eleType = null;
ExtractAParamModel modelParamA = null;
ExtractImgParamModel modelParamImg = null;
if (this.moduleCode == DevToolModuleCode.ExtractA.ToString())
{
eleType = "a";
modelParamA = this.pgParamObj.SelectedObject as ExtractAParamModel;
}
else if (this.moduleCode == DevToolModuleCode.ExtractImg.ToString())
{
eleType = "img";
modelParamImg = this.pgParamObj.SelectedObject as ExtractImgParamModel;
}
HtmlAgilityPack.HtmlDocument hDoc = NS.Utility.HtmlParse.HtmlParseUtil.LoadHtmlData(paramText);
HtmlAgilityPack.HtmlNodeCollection hnc = hDoc.DocumentNode.SelectNodes(".//" + eleType);
foreach (HtmlAgilityPack.HtmlNode hn in hnc)
{
if (this.moduleCode == DevToolModuleCode.ExtractA.ToString())
{
//string href = hn.Attributes["href"].Value;
var attriHerf = hn.Attributes["href"];
if (attriHerf == null)
continue;
string href = hn.Attributes["href"].Value;
string innerText = hn.InnerText.Trim();
if (modelParamA.ExtractHref)
{
string hrefValue = null;
string urlPrefix = modelParamA.UrlPrefix;
if (!string.IsNullOrEmpty(urlPrefix))
{
hrefValue = innerText + "," + urlPrefix + href;
}
else
{
hrefValue = innerText + "," + href;
}
if (!string.IsNullOrEmpty(modelParamA.OldReplace))
{
hrefValue = hrefValue.Replace(modelParamA.OldReplace, modelParamA.NewReplace);
}
listP.Add(hrefValue);
}
else
{
listP.Add(hn.OuterHtml);
}
}
else if (this.moduleCode == DevToolModuleCode.ExtractImg.ToString())
{
string url = hn.Attributes["src"].Value;
if (!string.IsNullOrEmpty(modelParamImg.ImgUrlPrefix))
{
url = modelParamImg.ImgUrlPrefix + url;
}
if (!string.IsNullOrEmpty(modelParamImg.OldReplace) && !string.IsNullOrEmpty(modelParamImg.NewReplace))
{
url = url.Replace(modelParamImg.OldReplace, modelParamImg.NewReplace);
}
listP.Add(url);
}
}
resultText = string.Join(Environment.NewLine, listP.ToArray());
另外,如果图片需要添加前缀,或者替换地址,都可以批量完成
看下工具的最终效果图




文章评论