other use google search
来源:互联网 发布:类似重生之星际淘宝主 编辑:程序博客网 时间:2024/05/20 06:26
The example simply takes a string that you want to search on http://www.google.com/ for and then displays the first five results that comes back. Nothing major but it does show an ever so slightly more complicated example than the Hello World example above. Anyway, once again, fire up your favorite text editor, type in the code below or copy it from the article and paste it in.
using System;using System.Net;using System.Web;using System.Text;using System.Text.RegularExpressions;namespace Dela.Mono.Examples{ class GoogleSearch { static void Main(string[] args) { Console.Write("Please enter string to search google for: "); string searchString = HttpUtility.UrlEncode(Console.ReadLine()); Console.WriteLine(); Console.Write("Please wait.../r"); // Query google. WebClient webClient = new WebClient(); byte[] response = webClient.DownloadData("http://www.google.com/search?&num=5&q=" + searchString); // Check response for results string regex = "g><a//shref=/"?(?<URL>[^/">]*)>(?<Name>[^<]*)"; MatchCollection matches = Regex.Matches(Encoding.ASCII.GetString(response), regex); // Output results Console.WriteLine("===== Results ====="); if(matches.Count > 0) { foreach(Match match in matches) { Console.WriteLine(HttpUtility.HtmlDecode( match.Groups["Name"].Value) + " - " + match.Groups["URL"].Value); } } else { Console.WriteLine("0 results found"); } } }}
Save this file as GoogleExample.cs and go back to the command line. The command to compile this application is a little different to the one we used for the Hello World example. This application makes use of the HttpUtility
class which is defined in the System.Web.dll assembly. That means we need to reference this assembly when compiling. To do this you simply use the -r
switch. This is the full command you need to type to get this application to compile: mcs GoogleExample.cs -r System.Web.dll
Once again, the previous command assumes that your command prompt is working in the same directory as the GoogleExample.cs file and that the Mono directories have been added to your path if you are using Windows.
As with the Hello World example you can run this application from the command line using mono GoogleExample.exe
or mint GoogleExample.exe
- Take your pick. The same compiled assembly will run on Windows and Linux. You can test this by compiling it on Windows, copying the assembly to a Linux machine and running the mono GoogleExample.exe
command there.
- other use google search
- 20 Tips To Use Google Search Efficiently
- How to use Google search engine
- use google
- use-google
- Search Google
- google search
- 如何有效的使用谷歌搜索引擎(How to Use Google Search More Effectively)
- How to use a man page Faster than a Google search
- GitHub, Google Code, and other
- use pipe to contact with each other
- Spirng Use In Other Times (补充)
- how to use google
- how to use Google
- Google Analytics流量分类中的Other
- Lotus Notes Search Use JSON
- Google Desktop Search
- google's map search
- OO数据结构之思考
- JSP/Servlet构建三层管理信息系统
- 觉得应该开始重新规划自己的人生了
- use google
- JSP调用JavaBean在网页上动态生成柱状图(转 )
- other use google search
- JSP与SQL SERVER的留言本
- HTTP POSTs and HTTP GETs with WebClient and C# and Faking a PostBack
- DOS下的EXE格式文件
- 使用JSP + JAVABEAN + XML 开发的一个例子
- SysTem in Programming
- 我的毕业设计的前期准备
- [转]男人靠什么吸引女人