加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

抓取正则DEMO

发布时间:2020-12-14 02:19:43 所属栏目:百科 来源:网络整理
导读:private void GetItemList(int task_ID,ListCrawlerResult arrayList,string group_url) { MatchCollection matchList; MatchCollection tempMatch; DownWebFile df = new DownWebFile(); var _html = df.GetPageData(group_url,"utf-8"); Regex regExtractH
private void GetItemList(int task_ID,List<CrawlerResult> arrayList,string group_url)
        {
            MatchCollection matchList;
            MatchCollection tempMatch;
            DownWebFile df = new DownWebFile();
            var _html = df.GetPageData(group_url,"utf-8");
            Regex regExtractHtml = new Regex(@"<table class=""olt"">[sS]+?</table>",RegexOptions.None);
            Regex regLi = new Regex(@"<tr class="""">[sS]+?</tr>",RegexOptions.None);
            Regex regexTitle = new Regex(@"<td class=""title"">[sS]+?</td>",RegexOptions.None);
            Regex regexHref = new Regex(@"<a [^>]+?>[sS]+?</a>",RegexOptions.None);
            Regex regexAuthor = new Regex(@"<td nowrap=""nowrap"">[sS]+?</td>",RegexOptions.None);
            matchList = regExtractHtml.Matches(_html);
            if (matchList.Count < 1) return;
            var _contentHtml = matchList[0].Value;
            matchList = regLi.Matches(_contentHtml);
            for (int i = 0; i < matchList.Count; i++)
            {
                var _text = matchList[i].Value;
                CrawlerResult item = new CrawlerResult();
                item.Task_ID = task_ID;
                tempMatch = regexTitle.Matches(_text);
                if (tempMatch.Count < 0) continue;
                item.Title = CommonFunction.DeleteHTMLElement(tempMatch[0].Value);
                tempMatch = regexHref.Matches(tempMatch[0].Value);
                if (tempMatch.Count < 0) continue;
                item.Url = GetURL(tempMatch[0].Value);
                if (item.Url.IndexOf("http://") < 0) continue;
                tempMatch = regexAuthor.Matches(_text);
                if (tempMatch.Count > 0)
                {
                    item.Author = CommonFunction.DeleteHTMLElement(tempMatch[0].Value);
                }
                item.SiteName = "XX";
                item.FilterType = IWOMWebCrawlerDbLayer.Common.FilterType.FilterNo;
                arrayList.Add(item);
            }
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读