问题描述
我得到一个简单的任务,它涉及到越来越追随者算一些Twitter账户的。有可能是我要创建的应用程序应该得到每个用户的追随者数量和总结起来1000年的Twitter用户名。
I was given a simple task which involves getting the followers count of a number of twitter accounts. There could be 1000 twitter usernames that the application i'm creating is supposed to get each user's followers count and sum them up.
看起来像一件容易的事,所以我所做的就是使用Twitter API调用(从C#客户端 )来获得信息,我需要为每个用户。由于信息后,我是没有什么特别的只是一些公开的数据,我用未经验证calls.Like这样的:
Seems like an easy task, so what I did was to use Twitter API calls (from a C# client) to get the info i need for each user. Since the info i'm after are nothing special just some public data, I used unauthenticated calls.Like this:
string target = "http:twitter.com/users/" + userName + ".xml";
Console.WriteLine("UserName: " + userName);
WebClient client = new WebClient();
Stream stream = client.OpenRead(target); StreamReader reader = new
StreamReader(stream);
XmlTextReader xml_read = new XmlTextReader(stream);
while (xml_read.Read()) {
xml_read.MoveToElement();
if (xml_read.Name == "name") {
Console.WriteLine("Name: " + xml_read.ReadInnerXml().ToString());
}
if (xml_read.Name == "followers_count"){
Console.WriteLine("Followers: " +
xml_read.ReadInnerXml().ToString());
Console.WriteLine();
}
}
此工作大大(在速度和sufficing目的而言)但是它具有与速率限制的问题。即我不能做超过100个左右的请求每小时..所以1000的用户,我将需要10个小时做这个简单的任务! ..
This works greatly (in terms of speed and sufficing the purpose) however it has the problem with rate limiting. i.e. i can't do more than 100 or so requests per hour .. so for 1000 user i will need 10 hours to do this simple task! ..
我试着走了不同的道路..因为我需要的信息都是公开的,我决定下载每个Twitter帐户的html页面来获得追随者(和真实姓名)。像这样:
I tried to go a different path.. since the info i needed are public I decided to download the html pages of each twitter account to get the followers (and real name). Like so:
WebRequest myWebRequest = WebRequest.Create("http://twitter.com/" + userName);
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(ReceiveStream, encode);
string strResponse = readStream.ReadToEnd();
然后,使用简单的字符串分析,以获得追随者数量和真实姓名。
Then using simple string parsing to get the followers count and real name.
此解决方案是有效的。但是下载整个HTML文件(大小为300KB +)是不是一个真正的聪明的方式来做到这一点。 PLUS大约需要2小时才能完成的任务。
This solution is valid.. however downloading the whole html file (300kb+ in size) is not really a smart way to do it. PLUS it takes roughly 2hrs to complete the task.
我是什么要求?
我希望/有信心有一个更聪明,更有效的方式,从Twitter获取公共信息。
I'm hoping/confident there is a smarter and more efficient way to get public info from Twitter.
推荐答案
也许你可以使用此解决方案:的
Maybe you can use this solution: https://dev.twitter.com/docs/api/1/get/users/lookup
它可以让你请求的数据多达100个用户名(逗号隔开)这样的能力:
的
It gives you the ability to request data for up to 100 usernames (comma separated) like this:https://api.twitter.com/1/users/lookup.json?screen_name=bloodyairtimer,geertvdc
您可以自己决定,如果你希望结果XML或JSON。也许JSON的规模将会比XML更小。
You can decide yourself if you would like the result in xml or json. Maybe the size of json will be smaller than xml.
这篇关于Twitter的速率限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!