Html parser 访问好友

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Chilkat;

namespace HtmlProcessor
{
  public  class XiaoNeiFriend
    {
        string friendName;
        string href;

      public string FriendName
      {
          get { return friendName; }
      }
      public string Href
      {
          get { return href; }
      }
        public XiaoNeiFriend(string name,string link)
        {
            friendName = name;
            href = link;
        }
        public override string ToString()
        {
            return friendName + " " + href;
            //return base.ToString();
        }
    }
    public class HtmlParser
    {
        //string htmlFileName;
        //string xmlFileName = @"d:/xiaonei/out_wang.xml";
        bool hasPrivacyLock=false;
        bool isBoy = true;//sex
        System.DateTime currentTime;
        Random rnd;

        public List<XiaoNeiFriend> friendList = new List<XiaoNeiFriend>();

        public bool IsLocked
        {
            get { return hasPrivacyLock; }
        }
        public bool IsBoy
        {
            get { return isBoy; }
        }
        public HtmlParser()//string fileName)
        {
        }
        public string AbstractFriends(string allText)
        {
            //StreamReader reader = new StreamReader(htmlFileName);
            //string allText=reader.ReadToEnd();

            hasPrivacyLock = false;
            isBoy = true;

            //未加只有好友才能访问限制
            int index = allText.IndexOf("<h4>他的好友</h4>");
            if (-1==index)
            {
                index = allText.IndexOf("<h4>她的好友</h4>");
                isBoy = false;
            }
            if (-1==index)
            {
                isBoy = true;
                //可能是自己的主页
                index = allText.IndexOf("<h4>好友</h4>");
                if (-1==index)
                {
                    //也可能是好友才能访问限制
                    index = allText.IndexOf("<h1>他的好友<span>");
                    if (-1==index)
                    {
                        index = allText.IndexOf("<h1>她的好友<span>");
                        if (-1==index)
                        {
                            isBoy = true;
                            Console.WriteLine("出现未知情况,可能这人没有好友!");
                            return "";
                        }
                        hasPrivacyLock = true;
                        isBoy = false;
                    }
                    hasPrivacyLock = true;
                }
            }
            allText=allText.Substring(index);
            int indexOfLi = allText.IndexOf("<li>");
            if (-1!=indexOfLi)
            {
                allText = allText.Substring(indexOfLi);
                int indexOfEndDiv = allText.IndexOf("</ul>");
                if (-1 != indexOfEndDiv)
                {
                    allText = allText.Substring(0, indexOfEndDiv);
                    //int indexOfEndLi = allText.LastIndexOf("</li>");
                    //if (-1!=indexOfEndLi)
                    //{
                    //    //具有好友锁定时的情况
                    //    //Console.WriteLine("Can't find </li>!");
                    //    //return "";
                    //    allText = allText.Substring(0, indexOfEndLi);
                    //}
                    //将之转化为良定义的xml文件
                    allText=allText.Replace("&", "&amp;");//转义&字符
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<root>/r/n");
                    sb.Append(allText);
                    sb.Append("/r/n</root>");
                    return sb.ToString();
                }
                else
                {
                    Console.WriteLine("Can't find </ul>!");
                    return "";
                }
            }
            else
            {
                Console.WriteLine("Can't find <li>!");
                return "";
            }
        }
        //获取所有的friend;暂时没有用到
          public bool GetAllFriends(string fn)
        {
            Chilkat.Xml xml = new Chilkat.Xml();
            if (!xml.LoadXml(fn))
            {
                Console.WriteLine("Failed to load xml string!");
                return false;
            }
            Chilkat.Xml Li = xml.FirstChild();
            while (Li!=null)
            {
                Chilkat.Xml link = Li.GetChildWithTag("a");
                if (null==link)
                {
                    return false;
                }
                string href=link.GetAttrValue("href");
                string title = link.GetAttrValue("title");
                if (String.IsNullOrEmpty(href)||string.IsNullOrEmpty(title))
                {
                    return false;
                }
                XiaoNeiFriend friend = new XiaoNeiFriend(title, href);
                friendList.Add(friend);

                Li = Li.NextSibling();
            }
            return true;
        }
        public XiaoNeiFriend GetARandomRecord(string fn)
        {
            Chilkat.Xml xml = new Chilkat.Xml();
            if (!xml.LoadXml(fn))
            {
                Console.WriteLine("Failed to load xml string!");
                return null;
            }

            int numOfChild = xml.NumChildren;
            //Console.WriteLine("num of children:{0}", numOfChild);
            if (numOfChild==0)
            {
                Console.WriteLine("This person do not has a friend!");
                return null;
            }
            currentTime = DateTime.Now;
           // Console.WriteLine(currentTime.Millisecond);
            rnd = new Random(currentTime.Millisecond);

            int nth = rnd.Next(numOfChild);
            //Console.WriteLine("rand={0}", nth);

            Chilkat.Xml nthRecd=xml.GetNthChildWithTag("li",nth);
            if (nthRecd != null)
            {
                Chilkat.Xml link =nthRecd.GetChildWithTag("a");
                if (null == link)
                {
                    return null;
                }
                string href = link.GetAttrValue("href");
                string title = link.GetAttrValue("title");
                if (String.IsNullOrEmpty(href) || string.IsNullOrEmpty(title))
                {
                    return null;
                }
                return new XiaoNeiFriend(title, href);
            }
            return null;
        }
            }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值