开发者

How to read a Google calculation query result using .net/c#?

开发者 https://www.devze.com 2023-02-02 20:16 出处:网络
I want to submit Google queries like these: http://www.google.ch/search?q=100+eur+to+chf http://www开发者_如何学JAVA.google.ch/search?q=1.5*17.5

I want to submit Google queries like these:

...from a C# console application and capture the result reported back by Google (and ignore any links to other sites). Is there a specific Google API that helps me with this task?

I got this idea from the tool Launchy (launchy.net). The plugin GCalc does this, I also found the source file for this module:

http://launchy.svn.sourceforge.net/viewvc/launchy/tags/2.5/plugins/gcalc/gcalc.cpp?revision=614&view=markup

It looks like GCalc does not use any Google API at all. But I've got no clue how to do the same in C#, and I would prefer to use a proper API. But if there isn't one, I could use some help/pointers on how to copy the GCalc functionality to C# (.net libraries/classes...?)


Google calculator results don't show up when using the API. So if you want them, you'll have to scrape the page. Be careful doing so as it's against Google' terms of service so your IP will be banned if you send too many frequent requests.

Once you've got the results page, use an html parser. The result is in a <b> tag (e.g. <b>1 + 1 = 2</b>; if it's not present, then you have no calculator result). Be careful of <sup> tags within the result (e.g. <b>(1 (m^2) kg) / 2 = 0.5 m<sup>2</sup> kg</b>). You might also want to decode the html entities.


You can use WebClient.DownloadString(String url). This way you get page (html) as string. You have to parse result, but that shouldn't be hard. HttpAgilityPack is good c# html parser that uses XPath for data retrieval.


why not use HTTPWebRequest and then parse the result as macrog stated in his answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号