Twitter 速率限制
我接到了一项简单的任务,其中涉及获取多个 Twitter 帐户的关注者数量。我正在创建的应用程序可能有 1000 个 Twitter 用户名,应该对每个用户的关注者进行计数并对它们进行求和。
看起来这是一项简单的任务,所以我所做的就是使用 Twitter API 调用(来自 C# 客户端)来获取每个用户所需的信息。由于我所追求的信息没什么特别的,只是一些公共数据,所以我使用了未经身份验证的调用。像这样:
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();
}
}
这非常有效(就速度和满足目的而言),但它存在速率限制问题。也就是说,我每小时不能执行超过 100 个左右的请求。因此,对于 1000 个用户,我需要 10 个小时来完成这个简单的任务! ..
我尝试走不同的道路.. 由于我需要的信息是公开的,我决定下载每个 Twitter 帐户的 html 页面以获取关注者(和真实姓名)。像这样:
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();
然后使用简单的字符串解析来获取关注者数量和真实姓名。
这个解决方案是有效的..但是下载整个 html 文件(大小超过 300kb)并不是一个明智的方法。另外,完成任务大约需要 2 小时。
我要什么?
我希望/相信有一种更智能、更有效的方式从 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.
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();
}
}
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! ..
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.
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.
What am I asking for ?
I'm hoping/confident there is a smarter and more efficient way to get public info from Twitter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您可以使用此解决方案: https://dev.twitter.com/ docs/api/1/get/users/lookup
它使您能够请求最多 100 个用户名(逗号分隔)的数据,如下所示:
https://api.twitter.com/1/users/lookup .json?screen_name=bloodyairtimer,geertvdc
您可以自行决定是否需要 xml 或 json 格式的结果。也许json的大小会比xml小。
Maybe you can use this solution: https://dev.twitter.com/docs/api/1/get/users/lookup
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
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 支持,但没有收到有用的回复。每个 API 方法都有一个速率限制,一般来说,如果您经过身份验证,您可以拥有更大的速率限制(公共方法为 150,经过身份验证为 350),但这并不是一个很大的价值。
如果您可以使用多个 IP 地址,则可以有不同的请求计数,但如果您调用具有身份验证的 API 会更好,因为这种情况下的计数不是针对 IP 的,而是针对用户的。
twitter 速率限制策略定义如下:
https://dev.twitter.com/docs/rate-limiting/faq
I've the same problem. I asked twitter support via developer forum, but I don't receive a usefull response. Every API method has a rate limit, in general if you are authenticated you can have a greater one (150 for public method, 350 for authenticated) but it's not a great value.
If you can use more than one IP address, you can have different request counts, but it's better if you call an API with authentication because the count in this case is not for IP but it's for user.
The twitter rate limit policy is defined here:
https://dev.twitter.com/docs/rate-limiting/faq
你也可以使用像 Gnip 这样的服务,
他们已经存储了所有的 twitter!
you could also use a service like Gnip
they have stored all of twitter!