获取博客园之博问

前端方法

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" id="viewport" name="viewport">
    <title></title>    
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
            list-style: none;
            box-sizing:border-box;
            font-size:1em;
        }
        .header {
            background: #ffcb20;
            border:1px solid #ccc;
            text-align:center;
            position:fixed;
            top:0;
            width:100%;
            line-height:3em;
            vertical-align:middle;
            z-index:10;
        }
        .toolbar{
            border-top:1px solid #ccc;
            position:fixed;
            right:0;
            left:0;
            background-color:#fff;
            width:100%;
            bottom:0px;
            line-height:2.5em;
        }
        .tab-item{
            border-right:1px solid #ddd;
            text-decoration:none;
            height:2em;
            color:#ccc;
            text-align:center;
            width:1%;
            display:table-cell;
            vertical-align:middle;
        }
        .tab-label{
            margin-top:2px;
            display:block;
            overflow:hidden;
        }
        .content{
            overflow-x:no-content;
            overflow-y:scroll;
            height:85%;
            position:fixed;
            width:100%;
            top:3em;
        }
        .content .content-items{
            width:100%;
        } 
        .content .content-item {
            margin-top: 5px;
            margin-left: 4px;
            padding: 0;
            display: block;
            padding:1px;
            border-bottom:1px dotted #ddd;
        }
        .answer-count {
            float: left;
            width: 4em;
            border: 1px solid #ddd;
            text-align: center;
            -webkit-border-radius: 3px;
            border-radius: 3px;
            margin-top: 1em;
            line-height: 1.4em;
            color: #999;
        }
        .answer-count-num {
            background-color: #eee;
        }
        .answer-count-word{
            border-top:1px solid #ccc;
        }
        .answer-item {
            float: left;
            margin-left:1em;
            width: 90%;
            line-height: 3em;
        }
        .answer-title{

        }
        .clear{
            clear:both;
        }
        .answered {
            color: #ff7b24;
        }
        .unanswered {
        }
        .answer-footer {
            width: 100%;
            font-size: 0.8em;
            line-height:2em;
        }
        .answer-footer a{
            text-decoration:none;
        }
        .answer-footer-tag {
            float: left;
            margin-right: 0.3em;
            
        }
        .answer-footer-tag a {
            margin-right: 1em;
            background-color: #ccc;
            border-radius: 6px;
            padding: 4px 2px;
            color: #075db3;
        }
        .answer-footer-tag a{

        }
        .answer-footer-author {
           float:right;
           width:14em;
        }
        .answer-footer-author a {
            color: #075db3;
            margin-right:3px;
        }
        .answer-footer-author span {
            margin-right: 3px;
        }
    </style>
</head>
<body>
    <div class="header">
        问题列表
    </div>
    <div class="content">
        <div class="content-items">
            <div class="content-item">
                <div class="answer-count"></div>
                <div class="answer-item">
                    <div class="answer-title">

                    </div>
                    <div class="answer-content">

                    </div>
                    <div class="answer-footer">
                        <div class="answer-footer-tag">
                        </div>
                        <div class="anwer-footer-author">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="toolbar">
        <a class="tab-item" onclick='getData("p")' href="javascript:void(0)">
            <span class="tab-label">上页</span>
        </a>
        <a class="tab-item" onclick='getData("n")' href="javascript:void(0)">
            <span class="tab-label">下页</span>
        </a>
    </div>
   <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        Date.prototype.getDiffMinute = function () {
            var d = this;
            var now = new Date();
            var m=now.getTime() - d;
            m = Math.round(m / (1000 * 60));
            var r = "";
            if (r < 60) {
                r =m+ "分钟前";
            }
            if (m >= 60 && m < 60 * 24) {
                r = Math.round(m/60)+"小时前";
            } else if (m >= 60 * 24) {
                r = Math.round(m /(60*24)) + "天前";
            }
            return r;
        }
        var pageIndex = 1;
        function getData(c) {
            if (c == "p") {
                pageIndex--;
                if (pageIndex == 0) { pageIndex = 1; return; }
            } else if (c == "n") {
                pageIndex++;
            }
            
            var loading = $('<div class="loading"><img src="../Content/Image/loading.gif" /></div>');
            $(".content").append(loading);
            //var url = "http://10.100.22.54:8095/api/NewsManager/GetQuestionList";
            var url = "http://10.100.22.54:8095/api/NewsManager/GetQuestionPage";
            $.get(url, { pageIndex: pageIndex }, function (data) {
                var content = $(".content-items");
                content.empty();
                $.each(data, function (key, value) {
                    var item = '<div class="content-item">';
                    if (value.AnswerCount > 0) {
                        item += '<div class="answer-count"><div class="answer-count-num answered">' + value.AnswerCount + '</div><div class="answer-count-word">回答数</div></div>';
                    } else {
                        item += '<div class="answer-count"><div class="answer-count-num unanswered">' + value.AnswerCount + '</div><div class="answer-count-word">回答数</div></div>';
                    }

                    item += '<div class="answer-item">';
                    item += '<div class="answer-title"><a href="javascript:void(0)">' + value.Title + '</a></div>';
                    item += '<div class="answer-content">' + value.Content + '</div>';
                    item += '<div class="answer-footer">';
                    item += '<div class="answer-footer-tag">'
                    $.each(value.TagList, function (i, value) {
                        item += '<a href="javascript:void(0)">' + value.Tag + '</a>';
                    })
                    item += '</div>';
                    item += '<div class="answer-footer-author">';
                    item += '<a href="' + value.HomePage + '">' + value.Author + '</a>';
                    item += '<span>游览(' + value.VisitTimes + ')</span>';
                    item += '<span>' + (new Date(value.Date)).getDiffMinute() + '</span>';
                    item += '</div>';
                    item += '</div>';
                    item += '</div>';
                    item += '</div>';
                    item += '<div class="clear"></div>';
                    content.append(item);
                })
            }, "json").done(function () {
                //loading.remove();
            })
        }
        $(function () {

            getData();
        })
    </script>
</body>
</html>

webapi方法

public HttpResponseMessage GetQuestionPage(int pageIndex=1)
        {
            NewsManage news = new NewsManage();
            return toJson(news.GetQuestionPage("http://q.cnblogs.com/list/unsolved",pageIndex));
        }

 

提供2中后台方法

public List<QuestionsModel> GetQuestionList(string url)
        {
            string result = "";
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(url);
                using (Stream stream = client.GetStreamAsync(url).Result)
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }
            return GetHtmlQuestions(result);
        }
        public List<QuestionsModel> GetQuestionPage(string url,int pageIndx)
        {
            string result = "";
            HttpWebRequest request = HttpWebRequest.Create(url + "?page="+pageIndx) as HttpWebRequest;
            request.Method = "get";
            request.ContentType = "text/html;chartset=utf-8";
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }
            return GetHtmlQuestions(result);
        }

 

对html正则表达式

public List<QuestionsModel> GetHtmlQuestions(string htmlContent)
        {
            List<QuestionsModel> newsList = new List<QuestionsModel>();
            StringBuilder sb = new StringBuilder();
            sb.Append("<div\\s*class=\"diggnum\\s*(answered|unanswered)\">(?<AnswerCount>[\\d]?)</div>\\s*");
            sb.Append("<div\\s*class=\"diggword\">回答数</div>\\s*");
            sb.Append("</div>\\s*");
            sb.Append("<div\\s*class=\"clear\">\\s*</div>\\s*");
            sb.Append("</div>\\s*");
            sb.Append("<div\\s*id=\"[^\"\"]*\"\\s*class=\"news_item\">\\s*");
            sb.Append("<h2\\s*class=\"news_entry\"\\s*>\\s*");
            sb.Append("(<span\\s*class=\"gold\"\\s*>(?<gold>[^<]*)</span>)?\\s*");//gold,不一定有悬赏,所以?,表示0或者1次
            sb.Append("(<img\\s*alt=\"[^\"\"]*\"\\s*src=\"[^\"\"]*\"\\s*/>)?\\s*");//表示悬赏的图标,要么不出现,要么出现1次
            sb.Append("<a\\s*target=\"_blank\"\\s*href=\"(?<href>[^\"\"]*)\">(?<title>[^<]*)</a>\\s*");//标题,点击标题后链接
            sb.Append("</h2>\\s*");
            sb.Append("<div\\s*class=\"news_summary\">(?<content>[^<]*)</div>\\s*");//具体内容
            sb.Append("<div\\s*class=\"news_footer\">\\s*<div\\s*style=\"[^\"\"]*\">\\s*(<a\\s*class=\"detail_tag\"\\s*href=\"[^\"\"]*\">(?<tag>[^<]*)</a>\\s*)*");//分类tag,*表示会出现0或者多个分类
            sb.Append("\\s*</div>\\s*");
            sb.Append("<div\\s*style=\"[^\"\"]*\"\\s*>\\s*");
            sb.Append("<a\\s*href=\"(?<HomePage>[^\"\"]*)\"\\s*class=\"author\">\\s*");//作者主页
            sb.Append("<img\\s*alt=\"[^\"\"]*\"\\s*src=\"[^\"\"]*\"\\s*/>\\s*");
            sb.Append("</a>\\s*");
            sb.Append("<a\\s*href=\"[^\"\"]*\"\\s*class=\"news_contributor\">(?<author>[^<]*)</a>\\s*");//作者姓名
            sb.Append("<br\\s*/>(?<VisitTimes>[^<span]*)<span\\s*title=\"(?<date>[^\"\"]*)\"\\s*class=\"date\">[^<]*</span>");//游览次数和发表时间
            string strPattern = sb.ToString();
            Regex regex = new Regex(strPattern, RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase);
            if (regex.IsMatch(htmlContent))
            {
                MatchCollection matchs = regex.Matches(htmlContent);
                foreach (Match match in matchs)
                {
                    var data = new QuestionsModel
                    {
                        AnswerCount = int.Parse(match.Groups["AnswerCount"].ToString()),
                        Title = match.Groups["title"].ToString(),
                        Gold = int.Parse(!string.IsNullOrWhiteSpace(match.Groups["gold"].ToString()) ? match.Groups["gold"].ToString() : "0"),
                        Content = match.Groups["content"].ToString(),
                        HomePage = match.Groups["HomePage"].ToString(),
                        Author = match.Groups["author"].ToString(),
                        Date = match.Groups["date"].ToString(),
                        TitleHref=match.Groups["href"].ToString(),
                        VisitTimes = int.Parse(Regex.Replace(match.Groups["VisitTimes"].ToString(), @"[^\d]+", ""))
                    };
                    data.TagList = new List<QuestionTag>();
                    for (var i = 0; i < match.Groups["tag"].Captures.Count; i++)
                    {
                        data.TagList.Add(new QuestionTag { Tag = match.Groups["tag"].Captures[i].Value });
                    }
                    
                    newsList.Add(data);
                }
            }
            return newsList;
        }

 

posted @ 2016-07-19 13:32  wjl910  阅读(72)  评论(0)    收藏  举报