时间:2021-07-01 10:21:17 帮助过:18人阅读
string json="上方的html";
第二步:通过正则表达式获取 <dl class="hello">内容
MatchCollection medl = Regex.Matches(json, @"<dl class=""hello"">([\s\S]*?)</dl>");//这里的json传的是需分析的字符串
 List<string> mclist = new List<string>();//用于存储最后遍历出来的实体数据
            //循环dl
            for (int i = 0; i < medl.Count; i++)
            {
  第三步:获取<dl>下<a>标签中的内容
                  //获取dl下的dt下的a标签
                  MatchCollection dedt = Regex.Matches(medl[i].Value, @"(?<=>).*(?=</a>)");
                  List<string> titlelist = new List<string>();
                  foreach (var item in dedt[0].Value.Split(‘,‘))//<dt><a>标签中包含两个内容,日记标题和日期,这里通过split分割将值遍历出来
                  {
                        titlelist.Add(item);
                  }
                  for (int b = 0; b < titlelist.Count; b++)
                  {
                        if (b == 0)
                        {
                              mclist.Insert(0, "日记标题:" + titlelist[b]);
                              mclist.Insert(1, "心情:" + dedt[1].Value);//第二个<a>标签的值
                        }
                        else if (b == 1)
                        {
                              mclist.Insert(2, "日期:" + titlelist[b]);
                        }
                        else
                        {
                        }
                  }
  第四步:获取<dl>下的<dd>标签里面的内容
                  //获取dl下的dd标签
                  MatchCollection mcdd = Regex.Matches(medl[i].Value, @"(?<=<dd>)([^<]*)(?=</dd>)");
                  //循环dd标签
                  for (int j = 0; j < mcdd.Count; j++)
                  {
                        mclist.Add(mcdd[j].Value);//将dd标签获取的值存入mclist中(如果这时存入的值有多余的转义字符可用Value.replace("需替换的值","替换后的值")替换)
                  }
                  hellobll.Add(GetModels(mclist));//将mclist中的数据存入数据库
                  //将mclist中存入数据库的数据移除,防止重复操作
                  mclist.Clear();
}
//该方法将数据对应的数据字段中,在this.GetValueByKey方法中拿到相应的值。
 private Model.hello GetModels(List<string> data)
        {
Model.hello model = new Model.hello ();
            model.Title= this.GetValueByKey(data, "日记标题");//日记标题
model.date= this.GetValueByKey(data, "日期");//日期
            model.Name = this.GetValueByKey(data, "记录人");//记录人
            model.Weather = this.GetValueByKey(data, "天气");//天气
model.Mood = this.GetValueByKey(data, "心情");//心情
            return model;
        }
        private string GetValueByKey(List<string> data, string key)
        {
            string result = data.Find(x => x.StartsWith(key));
            if (!string.IsNullOrEmpty(result))
            {
                result = result.Replace(key, string.Empty);
                result = result.Replace(":", "");
                result = result.Trim();
            }
            return result;
        }
//实体类
public class Diary
{
Public int ID{get;set;}
public string Title{get;set;}
public string Name{get;set;}
public string date{get;set;]//这里时间存的是string类型
public string Weather{get;set;}
public string Mood{get;set;}
}
正则表达式可优化,心情这条数据没有通过<dd>标签获取,是通过<a>标签获取的。
分析html格式数据,根据正则表达式获取所需数据,并存入数据库
标签:for str 重复 nbsp set 方法 public style class