当前位置: 移动技术网 > IT编程>开发语言>c# > C#获取HTML文本的第一张图片与截取内容摘要示例代码

C#获取HTML文本的第一张图片与截取内容摘要示例代码

2019年07月18日  | 移动技术网IT编程  | 我要评论

获取第一张图片

要我们获得到的数据是一段html文本、也许这段文本里面有许多图片、需要截取一张作为标题图片、也就是做为主图、这时就可以用到下面这个方法获取到第一张图片。

示例代码

#region 获取第一张图片
/// <summary>
/// 获取html文本的图片地址
/// </summary>
/// <param name="content"></param>
/// <returns></returns>/
/// 
public arraylist getimgurl(string html)
{
  arraylist resultstr = new arraylist();
  regex r = new regex(@"<img[^>] src=s*(?:´(?<src>[^´] )´|""(?<src>[^""] )""|(?<src>[^>s] ))s*[^>]*>", regexoptions.ignorecase);//忽视大小写
  matchcollection mc = r.matches(html);

  foreach (match m in mc)
  {
    resultstr.add(m.groups["src"].value.tolower());
  }
  if (resultstr.count > 0)
  {
    return resultstr;
  }
  else
  {
    resultstr.clear();
    return resultstr;
  }
}
#endregion

注意:上面所返回的是一个arraylist 集合、包含了文本里面所有的img的src、这样我们就可以访问到img的src

截取html文本

有时候我们得到的数据是一段html文本、需要截取html文本的一部分作为内容摘要、此时、我们可以运用下面这个方法

示例代码

 #region 新闻内容摘要
/// <summary>
/// 新闻内容摘要
/// </summary>
/// <param name="sstring"></param>
/// <param name="nleng"></param>
/// <returns></returns>
public string getcontentsummary(string content, int length, bool striphtml)
{
  if (string.isnullorempty(content) || length == 0)
    return "";
  if (striphtml)
  {
    regex re = new regex("<[^>]*>");
    content = re.replace(content, "");
    content = content.replace(" ", "").replace(" ", "");
    if (content.length <= length)
      return content;
    else
      return content.substring(0, length)  "……";
  }
  else
  {
    if (content.length <= length)
      return content;

    int pos = 0, npos = 0, size = 0;
    bool firststop = false, notr = false, noli = false;
    stringbuilder sb = new stringbuilder();
    while (true)
    {
      if (pos >= content.length)
        break;
      string cur = content.substring(pos, 1);
      if (cur == "<")
      {
        string next = content.substring(pos  1, 3).tolower();
        if (next.indexof("p") == 0 && next.indexof("pre") != 0)
        {
          npos = content.indexof(">", pos)  1;
        }
        else if (next.indexof("/p") == 0 && next.indexof("/pr") != 0)
        {
          npos = content.indexof(">", pos)  1;
          if (size < length)
            sb.append("<br/>");
        }
        else if (next.indexof("br") == 0)
        {
          npos = content.indexof(">", pos)  1;
          if (size < length)
            sb.append("<br/>");
        }
        else if (next.indexof("img") == 0)
        {
          npos = content.indexof(">", pos)  1;
          if (size < length)
          {
            sb.append(content.substring(pos, npos - pos));
            size = npos - pos  1;
          }
        }
        else if (next.indexof("li") == 0 || next.indexof("/li") == 0)
        {
          npos = content.indexof(">", pos)  1;
          if (size < length)
          {
            sb.append(content.substring(pos, npos - pos));
          }
          else
          {
            if (!noli && next.indexof("/li") == 0)
            {
              sb.append(content.substring(pos, npos - pos));
              noli = true;
            }
          }
        }
        else if (next.indexof("tr") == 0 || next.indexof("/tr") == 0)
        {
          npos = content.indexof(">", pos)  1;
          if (size < length)
          {
            sb.append(content.substring(pos, npos - pos));
          }
          else
          {
            if (!notr && next.indexof("/tr") == 0)
            {
              sb.append(content.substring(pos, npos - pos));
              notr = true;
            }
          }
        }
        else if (next.indexof("td") == 0 || next.indexof("/td") == 0)
        {
          npos = content.indexof(">", pos)  1;
          if (size < length)
          {
            sb.append(content.substring(pos, npos - pos));
          }
          else
          {
            if (!notr)
            {
              sb.append(content.substring(pos, npos - pos));
            }
          }
        }
        else
        {
          npos = content.indexof(">", pos)  1;
          sb.append(content.substring(pos, npos - pos));
        }
        if (npos <= pos)
          npos = pos  1;
        pos = npos;
      }
      else
      {
        if (size < length)
        {
          sb.append(cur);
          size ;
        }
        else
        {
          if (!firststop)
          {
            sb.append("……");
            firststop = true;
          }
        }
        pos ;
      }

    }
    return sb.tostring();
  }
}
#endregion

总结

以上就是利用c#获取一段html文本中的第一张图片和截取内容摘要的全部内容,希望本文的内容对大家学习或者使用c#能有所帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网