C#-407
class B{void Main(string[] a){var o=0;for(int i=1;i<11;i++){var r=((System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(new Uri(string.Format(a[0]+"&page={0}",i)))).GetResponse();if(r.ContentLength>0){using(var s=new StreamReader(r.GetResponseStream()))foreach(Match m in Regex.Matches(s.ReadToEnd(),"bounty worth (.+?) "))o+=int.Parse(m.Value.Substring(m.Value.IndexOf('h')+2));}}Console.Write(o);}}
使用Stackoverflow.com。除没有Gzip解压缩和不同的正则表达式外,其余与以下相同。
测试
> prog.exe http://stackoverflow.com/questions?pagesize=50&sort=featured
38150
奇怪的是,获得了与下面不同的值。
C#-496
这将使用api.stackexchange和json。
using System.IO.Compression;class B{void Main(string[] a){var o=0;for(int i=1;i<11;i++){var r=((System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(new Uri(string.Format(a[0]+"&page={0}",i)))).GetResponse();if(r.ContentLength>0)using(var s=new StreamReader(new GZipStream(r.GetResponseStream(),CompressionMode.Decompress)))foreach(Match m in Regex.Matches(s.ReadToEnd(),@"bounty_amount"":(.+?),"))o+=int.Parse(m.Value.Substring(m.Value.IndexOf(':')+1).Replace(",",""));}Console.Write(o);}}
未缩小:
using System.IO.Compression;
class B
{
void Main(string[] a)
{
var o = 0;
for (int i=1; i<11; i++) {
var w = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(new Uri(string.Format(a[0]+"&page={0}",i)));
if(w.GetResponse().ContentLength > 0)
using(var s = new StreamReader(new GZipStream(w.GetResponse().GetResponseStream(),CompressionMode.Decompress)))
foreach(Match m in Regex.Matches(s.ReadToEnd(), @"bounty_amount"":(.+?),"))
o += int.Parse(m.Value.Substring(m.Value.IndexOf(':')+1).Replace(",", ""));
}
Console.Write(o);
}
}
测试
默认页面大小:
> prog.exe http://api.stackexchange.com/2.2/questions/featured?site=stackoverflow
25300
页面大小= 100:
> prog.exe "http://api.stackexchange.com/2.2/questions/featured?site=stackoverflow&pagesize=100"
37400