博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【学习】爬糗事百科,可自动翻页。
阅读量:4562 次
发布时间:2019-06-08

本文共 4911 字,大约阅读时间需要 16 分钟。

1 namespace HTML  2 {  3     class Program  4     {  5         const string qsbkMainUrl = "http://www.qiushibaike.com";  6   7         private static string GetWBJokeUrl(int pageIndex)  8         {  9  10             StringBuilder url = new StringBuilder(); 11  12             url.Append(qsbkMainUrl); 13  14             url.Append("/textnew/page/"); 15  16             url.Append(pageIndex.ToString()); 17  18             url.Append("/?s=4869039"); 19  20             return url.ToString(); 21  22         } 23  24         //根据网页的url获取网页的html源码 25  26  27         private static string GetUrlContent(string url) 28         { 29             try 30             { 31  32                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 33  34                 request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.8.1000 Chrome/30.0.1599.101 Safari/537.36"; 35  36                 request.Method = "GET"; 37  38                 request.ContentType = "text/html;charset=UTF-8"; 39  40                 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 41  42                 Stream myResponseStream = response.GetResponseStream(); 43  44                 StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));//因为知道糗百网页的编码方式为utf-8 45  46                 string retString = myStreamReader.ReadToEnd(); 47  48                 myStreamReader.Close(); 49  50                 myResponseStream.Close(); 51  52                 return retString; 53  54             } 55  56             catch { return null; } 57  58         } 59         static void Main(string[] args) 60         { 61             System.Threading.Timer threadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(Method3), null, 0, 5000); 62             while (true) 63             { 64                 Thread.Sleep(1000); 65             } 66         } 67  68        static int first = 1; 69        static int curNum=1; 70        static void Method3(Object state) 71         { 72             List
a = GetJokeList(first == 1 ? curNum : first); 73 int i = 1; 74 Console.Clear(); 75 foreach (JokeItem item in a) 76 { 77 Console.WriteLine("笑话" + i + ":" + item.JokeContent + "\n"); 78 i++; 79 } 80 curNum++; 81 } 82 public class JokeItem 83 { 84 85 private string nickName; 86 87 ///
88 89 /// 昵称 90 91 /// 92 93 public string NickName 94 { 95 96 get { return nickName; } 97 98 set { nickName = value; } 99 100 }101 102 103 104 private Image headImage;105 106 ///
107 108 /// 头像109 110 /// 111 112 public Image HeadImage113 {114 115 get { return headImage; }116 117 set { headImage = value; }118 119 }120 121 private string jokeContent;122 123 ///
124 125 /// 笑话内容126 127 /// 128 129 public string JokeContent130 {131 132 get { return jokeContent; }133 134 set { jokeContent = value; }135 136 }137 138 139 140 private string jokeUrl;141 142 ///
143 144 /// 笑话地址145 146 /// 147 148 public string JokeUrl149 {150 151 get { return jokeUrl; }152 153 set { jokeUrl = value; }154 155 }156 157 }158 159 ///
160 161 /// 获取笑话列表162 163 /// 164 165 ///
166 167 public static List
GetJokeList(int pageIndex)168 {169 170 string htmlContent = GetUrlContent(GetWBJokeUrl(pageIndex));171 List
jokeList = new List
();172 Regex rg = new Regex("
\\s*((.*|
)*)", RegexOptions.IgnoreCase);173 174 JokeItem joke;175 176 MatchCollection matchResults = rg.Matches(htmlContent);177 178 179 180 foreach (Match result in matchResults)181 {182 joke = new JokeItem();183 joke.JokeContent = result.Groups[0].Value.Replace("
", "").Replace("", "").Replace("
", "").Replace("
","");184 joke.JokeContent = Regex.Replace(joke.JokeContent, @"(\r\n)+|(\r)+", "");//去掉多余的空行185 joke.JokeContent = Regex.Replace(joke.JokeContent, @"(\n)+", "\n");186 jokeList.Add(joke);187 }188 189 return jokeList;190 191 }192 193 194 195 196 }197 }
控制台代码

 

转载于:https://www.cnblogs.com/Zhengxue/p/8777981.html

你可能感兴趣的文章
2018/12/01 一个64位操作系统的实现 第四章 导入kernel.bin(4)
查看>>
如何在windows xp professional安装iis的解决方法
查看>>
抽象类和接口
查看>>
使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender实现自动完成功能(下)
查看>>
golang 常见疑惑总结
查看>>
8大你不得不知的Android调试工具
查看>>
pc端元素拖拽
查看>>
Sublime Text3使用Package Control 报错There Are No Packages Available For Installation
查看>>
判断连通图是否有环(并查集)
查看>>
汽车之家面试题2016
查看>>
POJ-数据结构-优先队列模板
查看>>
【HAOI2006】旅行(并查集暴力)
查看>>
css实现文本超出部分省略号显示
查看>>
留言板
查看>>
vue-router组件状态刷新消失的问题
查看>>
Android UI开发第十四篇——可以移动的悬浮框
查看>>
java8的一些用法
查看>>
(十)Hive分析窗口函数(二) NTILE,ROW_NUMBER,RANK,DENSE_RANK
查看>>
2018-11-19站立会议内容
查看>>
第五次作业 关于《构建之法》的心得体会
查看>>