获取新浪读书频道,书的列表程序

None.gif using  System;
None.gif
using  System.Net;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
using  System.Collections;
None.gif
using  System.Text.RegularExpressions;
None.gif
None.gif
namespace  ConsoleApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Class1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class Class1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string url="http://book.sina.com.cn/nzt/lit/dixiashi/index.shtml";
InBlock.gif            
string content=Gethtml(url);
InBlock.gif
InBlock.gif            
string[,] arr=new string[85,85];
InBlock.gif            arr[
0,0]="";
InBlock.gif            
for(int i=1;i<=84;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                url
="<a href=/nzt/lit/dixiashi/"+i+".shtml target=_blank class=a03>";
InBlock.gif                
string temp_1=GetChinese("[\u4e00-\u9fa5]",MID(content,url,80));//链接文字
InBlock.gif
                string temp_2="Books_zg_"+i+".html";
InBlock.gif                arr[i,
0]=temp_1;
InBlock.gif                arr[
0,i]=temp_2;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            content
=Getlist(4,700,arr);
InBlock.gif            Writefile(
@"C:\Documents and Settings\Administrator\桌面\index.html",content);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static string MID(string Content,string StartString,int length)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string Intercept=Content;
InBlock.gif            
int a=Intercept.IndexOf(StartString);
InBlock.gif            
string aa=Intercept.Substring(a,length);
InBlock.gif            
return aa;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取网页html代码
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="url"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private static string Gethtml(string url)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            WebClient wc
=new WebClient();
InBlock.gif            Stream str
=wc.OpenRead(url);
InBlock.gif            StreamReader sr
=new StreamReader(str,System.Text.Encoding.GetEncoding("GB2312"));
InBlock.gif            
return sr.ReadToEnd();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 获取指定网页的HTML代码
InBlock.gif
        static string GetPageSource(string URL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Uri uri 
=new Uri(URL);
InBlock.gif
InBlock.gif            HttpWebRequest hwReq 
= (HttpWebRequest)WebRequest.Create(uri);
InBlock.gif            HttpWebResponse hwRes 
= (HttpWebResponse)hwReq.GetResponse();
InBlock.gif
InBlock.gif            hwReq.Method 
= "Get";
InBlock.gif
InBlock.gif            hwReq.KeepAlive 
= false;
InBlock.gif
InBlock.gif            StreamReader reader 
= new StreamReader(hwRes.GetResponseStream(),System.Text.Encoding.GetEncoding("GB2312"));
InBlock.gif
InBlock.gif            
return reader.ReadToEnd();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取列表
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="col">列数</param>
InBlock.gif        
/// <param name="ww">table的宽度</param>
InBlock.gif        
/// <param name="arr">数组</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        static string  Getlist(int col,int ww,string[,] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int temp_1=arr.GetLength(0);
InBlock.gif            
int row=(int)System.Math.Ceiling(temp_1/Convert.ToDouble(col));//行数
InBlock.gif

InBlock.gif            
int temp_2=0;
InBlock.gif
InBlock.gif            
int temp_3=(int)System.Math.Floor(ww/Convert.ToDouble(col));//得到每列的宽度
InBlock.gif

InBlock.gif
InBlock.gif            StringBuilder sb
=new StringBuilder();
InBlock.gif            sb.Append(
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"><title>书的列表</title><style type=\"text/css\"><!--BODY {SCROLLBAR-FACE-COLOR: #c5c5c5; MARGIN: 0px; FONT: 12px 宋体; SCROLLBAR-HIGHLIGHT-COLOR: #c5c5c5; SCROLLBAR-SHADOW-COLOR: #c5c5c5; SCROLLBAR-3DLIGHT-COLOR: #c5c5c5; SCROLLBAR-ARROW-COLOR: #ffffff; SCROLLBAR-TRACK-COLOR: #fffffd; SCROLLBAR-DARKSHADOW-COLOR: #c5c5c5;font-size:13px;}A.a03:link {COLOR: #1E1E9C; TEXT-DECORATION: underline}A.a03:visited {    COLOR: #6d6e71; TEXT-DECORATION: none}A.a03:active {COLOR: #ff0000; TEXT-DECORATION: none}A.a03:hover {COLOR: #ff0000; TEXT-DECORATION: none}td{font-size:13px;}--></style></head><body><table width=\""+ww+"\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
InBlock.gif            
for(int i=1;i<=row;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(
"<tr>");
InBlock.gif                
for(int j=0;j<col;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    temp_2
++;
InBlock.gif                    sb.Append(
"<td width=\""+temp_3+"\">");
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sb.Append(
"<a href=\""+arr[0,temp_2]+"\" target=\"_blank\" class=\"a03\">"+arr[temp_2,0]+"</a>");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    sb.Append(
"</td>");
ExpandedSubBlockEnd.gif                }

InBlock.gif                sb.Append(
"</tr>");
InBlock.gif                sb.Append(
"<tr><td colspan=\""+col+"\" height=\"7\"></td></tr>");
ExpandedSubBlockEnd.gif            }

InBlock.gif            sb.Append(
"</table></body></html>");
InBlock.gif
InBlock.gif            
return sb.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 写文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="path"></param>
ExpandedSubBlockEnd.gif        
/// <param name="text"></param>

InBlock.gif        private static void Writefile(string path,string text)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using(StreamWriter sw=new StreamWriter(path,false,System.Text.Encoding.GetEncoding("gb2312"))) //中文,QuickCHM支持
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                sw.Write(text);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 提取HTML代码中的网址
InBlock.gif
        static ArrayList GetHyperLinks(string htmlCode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ArrayList al 
= new ArrayList();
InBlock.gif
InBlock.gif            
string strRegex = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
InBlock.gif
InBlock.gif            Regex r 
= new Regex(strRegex,RegexOptions.IgnoreCase);
InBlock.gif            MatchCollection m 
= r.Matches(htmlCode);
InBlock.gif
InBlock.gif            
for(int i=0; i<=m.Count-1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
bool rep = false;
InBlock.gif                
string strNew = m[i].ToString();
InBlock.gif
InBlock.gif                
// 过滤重复的URL
InBlock.gif
                foreach(string str in al)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(strNew==str)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        rep 
=true;
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }
   
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
if(!rep) al.Add(strNew);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            al.Sort();
InBlock.gif
InBlock.gif            
return al;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
static string GetChinese(string reg,string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int temp=str.IndexOf("(");
InBlock.gif            
string temp_1=str.Substring(temp+1,1);
InBlock.gif            Regex r 
= new Regex(reg,RegexOptions.IgnoreCase);
InBlock.gif            MatchCollection m 
= r.Matches(str);
InBlock.gif            
InBlock.gif            
string strNew="";
InBlock.gif            
for(int i=0; i<=m.Count-1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strNew 
+= m[i].ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return strNew+"("+temp_1+")";
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/wang123/archive/2006/12/25/602777.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值