出于工作需要,做了一个自动获取baidu的歌曲top100,top500的搜索排名的小东东.
程序只有一页,就是baidu.cs文件. 大约200行代码.
下面先附上源代码下载地址:/Files/mazhiyuan/Baidu.rar
需要说明的是:xml文件必须放在E盘根目录下,如果要换其它地方,你就得自己更换路径,并重新编译.
唉,水平象我这么菜的菜鸟,就爱把东西拿出去炫.^_^,高手反倒是不大炫的.
下面贴上baidu.cs的代码:
程序只有一页,就是baidu.cs文件. 大约200行代码.
下面先附上源代码下载地址:/Files/mazhiyuan/Baidu.rar
需要说明的是:xml文件必须放在E盘根目录下,如果要换其它地方,你就得自己更换路径,并重新编译.
唉,水平象我这么菜的菜鸟,就爱把东西拿出去炫.^_^,高手反倒是不大炫的.
下面贴上baidu.cs的代码:
1 using System;
2 using System.Text;
3 using System.Net;
4 using System.Text.RegularExpressions;
5
6 namespace Baidu
7 {
8 /// <summary>
9 /// Baidu 的摘要说明。
10 /// </summary>
11 public class Baidu
12 {
13 public Baidu()
14 {
15 //
16 // TODO: 在此处添加构造函数逻辑
17 //
18 }
19
20 public string GetContent(string url)
21 {
22 System.Net.HttpWebRequest req;
23 System.Net.HttpWebResponse res;
24
25 string startHtml = "<input type=hidden name=refreshtime value=@+RESBODY+@>";
26 string endHtml = "<input type=hidden name=refreshtime value=@-RESBODY-@>";
27
28 string html = "";
29
30 try
31 {
32 req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
33 res = (System.Net.HttpWebResponse)req.GetResponse();
34 }
35 catch
36 {
37 return System.Windows.Forms.MessageBox.Show("url定位错误").ToString();
38 }
39
40 System.IO.StreamReader sr = new System.IO.StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding(54936));
41 System.String wrStr;
42 wrStr = "";
43 do
44 {
45 wrStr = sr.ReadLine();
46 if(wrStr == null)
47 {
48 sr.Close();
49 }
50
51 html += wrStr;
52 }
53 while(wrStr!=null);
54
55 int a;
56 int b;
57 a = html.IndexOf(startHtml);
58 b = html.IndexOf(endHtml);
59
60 html = html.Substring(a, b-a);
61
62 return this.SubTable(html);
63 }
64
65 public string SubTable(string html)
66 {
67 System.Text.RegularExpressions.Regex reg = new Regex(">\\d{1,3}.<|_blank>\\S*</a>");
68 System.Text.RegularExpressions.MatchCollection matches = reg.Matches(html);
69
70 string range = "";
71 System.Text.StringBuilder tempStr = new StringBuilder();
72
73 string tempValue= "";
74 bool isNumeric = false;
75 for(int i=1; i<matches.Count; i++)
76 {
77 range = matches[i-1].ToString().TrimEnd('<').TrimStart('>').Replace(".", "").Replace("_blank>", "").Replace("</a>", "");
78
79 isNumeric = this.IsNumeric(range);
80
81 if ( isNumeric )
82 {
83 range = "$" + range;
84 }
85 else
86 {
87 range = "|" + range;
88 }
89
90 tempValue += range;
91 }
92
93
94 return "<table><tr><td>" + this.Result(tempValue) + "</td></tr></table>";
95 }
96
97 private bool IsNumeric(string match)
98 {
99 try
100 {
101 Convert.ToInt32(match);
102 }
103 catch
104 {
105 return false;
106 }
107
108 return true;
109 }
110
111 private string Result(string tempValue)
112 {
113 string[] resultArray = tempValue.Split('$');
114 System.Text.StringBuilder tempStr = new StringBuilder();
115 XMLHandler xmlHandler = new XMLHandler();
116
117 tempStr.Append("<tr>");
118 for(int i=1; i<resultArray.Length-1; i++)
119 {
120 if(resultArray[i]!="")
121 {
122 string[] tempArray = resultArray[i].Split('|');
123
124 if ( tempArray.Length>1 )
125 {
126 string id = tempArray[0];
127 string name = tempArray[1];
128 string link = "";
129
130 if(tempArray.Length==3)
131 {
132 link = tempArray[2];
133 }
134
135 tempStr.Append("<td>" + ReplaceStr(id) + "</td>" + "<td><a href=\"#1\" onclick=\"mlink(" + xmlHandler.LoadXML(name) + ")\" title=" + ReplaceStr(name) + ">" + ReplaceStr(name) + "(" + ReplaceStr(link) + ")</a></td>");
136 }
137
138 if(i%3==0)
139 {
140 tempStr.Append("</tr>");
141 }
142 }
143 }
144
145 return tempStr.ToString();
146 }
147
148 private string ReplaceStr(string replace)
149 {
150 if(replace == "")
151 {
152 return "";
153 }
154
155 return replace.Replace("|", "").Replace("$", "");
156 }
157
158 }
159
160 internal class XMLHandler
161 {
162 System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
163
164 internal string LoadXML(string songName)
165 {
166 string songN;
167 xmlDoc.Load(@"E:\Baidu.xml");
168
169 System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode("//Baidu/Song/SongName[text()='" + songName + "']");
170
171 if ( xmlNode == null )
172 {
173 songN = "$$";
174 }
175 else
176 {
177 songN = xmlNode.NextSibling.InnerText;
178 }
179
180 return songN;
181 }
182 }
183 }
184
2 using System.Text;
3 using System.Net;
4 using System.Text.RegularExpressions;
5
6 namespace Baidu
7 {
8 /// <summary>
9 /// Baidu 的摘要说明。
10 /// </summary>
11 public class Baidu
12 {
13 public Baidu()
14 {
15 //
16 // TODO: 在此处添加构造函数逻辑
17 //
18 }
19
20 public string GetContent(string url)
21 {
22 System.Net.HttpWebRequest req;
23 System.Net.HttpWebResponse res;
24
25 string startHtml = "<input type=hidden name=refreshtime value=@+RESBODY+@>";
26 string endHtml = "<input type=hidden name=refreshtime value=@-RESBODY-@>";
27
28 string html = "";
29
30 try
31 {
32 req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
33 res = (System.Net.HttpWebResponse)req.GetResponse();
34 }
35 catch
36 {
37 return System.Windows.Forms.MessageBox.Show("url定位错误").ToString();
38 }
39
40 System.IO.StreamReader sr = new System.IO.StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding(54936));
41 System.String wrStr;
42 wrStr = "";
43 do
44 {
45 wrStr = sr.ReadLine();
46 if(wrStr == null)
47 {
48 sr.Close();
49 }
50
51 html += wrStr;
52 }
53 while(wrStr!=null);
54
55 int a;
56 int b;
57 a = html.IndexOf(startHtml);
58 b = html.IndexOf(endHtml);
59
60 html = html.Substring(a, b-a);
61
62 return this.SubTable(html);
63 }
64
65 public string SubTable(string html)
66 {
67 System.Text.RegularExpressions.Regex reg = new Regex(">\\d{1,3}.<|_blank>\\S*</a>");
68 System.Text.RegularExpressions.MatchCollection matches = reg.Matches(html);
69
70 string range = "";
71 System.Text.StringBuilder tempStr = new StringBuilder();
72
73 string tempValue= "";
74 bool isNumeric = false;
75 for(int i=1; i<matches.Count; i++)
76 {
77 range = matches[i-1].ToString().TrimEnd('<').TrimStart('>').Replace(".", "").Replace("_blank>", "").Replace("</a>", "");
78
79 isNumeric = this.IsNumeric(range);
80
81 if ( isNumeric )
82 {
83 range = "$" + range;
84 }
85 else
86 {
87 range = "|" + range;
88 }
89
90 tempValue += range;
91 }
92
93
94 return "<table><tr><td>" + this.Result(tempValue) + "</td></tr></table>";
95 }
96
97 private bool IsNumeric(string match)
98 {
99 try
100 {
101 Convert.ToInt32(match);
102 }
103 catch
104 {
105 return false;
106 }
107
108 return true;
109 }
110
111 private string Result(string tempValue)
112 {
113 string[] resultArray = tempValue.Split('$');
114 System.Text.StringBuilder tempStr = new StringBuilder();
115 XMLHandler xmlHandler = new XMLHandler();
116
117 tempStr.Append("<tr>");
118 for(int i=1; i<resultArray.Length-1; i++)
119 {
120 if(resultArray[i]!="")
121 {
122 string[] tempArray = resultArray[i].Split('|');
123
124 if ( tempArray.Length>1 )
125 {
126 string id = tempArray[0];
127 string name = tempArray[1];
128 string link = "";
129
130 if(tempArray.Length==3)
131 {
132 link = tempArray[2];
133 }
134
135 tempStr.Append("<td>" + ReplaceStr(id) + "</td>" + "<td><a href=\"#1\" onclick=\"mlink(" + xmlHandler.LoadXML(name) + ")\" title=" + ReplaceStr(name) + ">" + ReplaceStr(name) + "(" + ReplaceStr(link) + ")</a></td>");
136 }
137
138 if(i%3==0)
139 {
140 tempStr.Append("</tr>");
141 }
142 }
143 }
144
145 return tempStr.ToString();
146 }
147
148 private string ReplaceStr(string replace)
149 {
150 if(replace == "")
151 {
152 return "";
153 }
154
155 return replace.Replace("|", "").Replace("$", "");
156 }
157
158 }
159
160 internal class XMLHandler
161 {
162 System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
163
164 internal string LoadXML(string songName)
165 {
166 string songN;
167 xmlDoc.Load(@"E:\Baidu.xml");
168
169 System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode("//Baidu/Song/SongName[text()='" + songName + "']");
170
171 if ( xmlNode == null )
172 {
173 songN = "$$";
174 }
175 else
176 {
177 songN = xmlNode.NextSibling.InnerText;
178 }
179
180 return songN;
181 }
182 }
183 }
184
生成出来的是html代码,把它copy到notepad上,另存为.htm文件即可.
http://mazhiyuan.cnblogs.com/archive/2006/04/06/367991.html
浙公网安备 33010602011771号