[Open the engine]
I often habitually open the homepage of my blog and read some wonderful technologies.ArticleThe technical atmosphere here is very good, and everyone's enthusiasm for Writing blog posts has always infected me.
Today, at noon, I suddenly had an idea: Count the response rate of these blog posts on the blog homepage. Because we had a one-hour break at noon, we just made a general function implementation, without much modification or input verification.
[Development implementation]
The implementation process is also very simple.
1. Create a winform project, draw a simple form, and drag several controls.
"How many pages are counted" means the number of pages on the homepage of the blog garden to be counted.
II. Implementation ideas:
1. Construct a list to store the pagination URL for Statistics
2. Obtain HTML for each URLSource codeThen, the number of comments and the number of reads are retrieved by string retrieval, locating, stripping, and re-retrieval, and saved as a key-value pair to a dictionary <>.
3. collect statistics on the data in dictionary <>, splice the string, and display it to textbox.
III,CodePart
Dictionary < Int , Int > Commentandread = New Dictionary < Int , Int > (); Private Int Totalcomment; Private Int Totalread; Private Void Tongji_click ( Object Sender, eventargs e ){ // Construct multiple URLs based on the number of pages. Int Pagecount = convert. toint32 ( This . Txtpage. Text); List < String > Pageurls = New List < String > (); For ( Int I = 1 ; I <= pagecount; I ++){ If (I = 1 ){ String Url = @" Http://www.cnblogs.com/index.html " ; Pageurls. Add (URL );} Else { String Url = @" Http://www.cnblogs.com/ " + " P " + I; pageurls. Add (URL );}} // Read HTML code by URL Foreach ( VaR URL In Pageurls ){ String Strhtml ="" ; WebClient WC = New WebClient (); # Region Because the company needs to use a proxy to access the Internet, you need to configure the proxy server first. If you directly access the Internet, you can skip this section. System. net. WebProxy owebproxy = New System. net. WebProxy ( " 172.16.154.100 " , 8080 ); Owebproxy. usedefaultcredentials =True ; WC. Proxy = Owebproxy; # Endregion WC. Encoding = Encoding. utf8; // Character encoding Strhtml = WC. downloadstring (URL ); // Search for comments and readings in HTML Htmldispose (strhtml );} // Statistics, concatenated strings, and output Foreach ( VaR C In Commentandread) {totalread + = C. Key; totalcomment + = C. Value;} stringbuilder sb = New Stringbuilder (); sb. appendformat ( " {0} pages retrieved in total, {1} blog posts \ r \ n " ,This . Txtpage. Text, commentandread. Count. tostring (); sb. appendline ( " Reading volume: " + Totalread); sb. appendline ( " Comments: " + Totalcomment); sb. appendline ( " Response rate: " + (Convert. todouble (totalcomment)/convert. todouble (totalread ))*100 ). Tostring ( " 0.00 " ) + " % " ); This . Textbox2.text = SB. tostring ();} Private Void Htmldispose ( String Html ){ Int Commentstart = html. indexof ( " Comment (<span id = " ); If (Commentstart! =- 1 ){ Int Commentend = html. indexof ( " </Span>) </a> </span> <SPAN class = " ); Int Comment = convert. toint32 (html. substring (commentstart +37 , Commentend-commentstart- 37 )); String Htmlcut = Html. substring (commentend ); Int Readstart = htmlcut. indexof ( " Read (<span id = " ); Int Readend = htmlcut. indexof ( " </Span>) </a> </span> </div> " ); Int Read = convert. toint32 (htmlcut. substring (readstart + 32 , Readend-readstart- 32 )); Try {Commentandread. Add (read, comment );} Catch { // The number of blog posts with the same read volume exists. skip this step. } String Htmlretain = Htmlcut. substring (readend); htmldispose (htmlretain );} Else { Return ;}}
[Tool demo]
[Summary]
According to statistics, the response rate of blog posts on the blog homepage is about 5‰. I personally think it is not very high. I hope you can reply a lot, your reply is the motivation for bloggers to launch more originality!
Author: Tony Zhao
Source of this article: http://www.cnblogs.com/ytaozhao copyright belongs to the author and blog Park, welcome to reprint, but without the author's consent must retain this paragraph of the statement, and in the Article Page clearly given the original connection, otherwise, you are entitled to pursue legal liability.