我正在为Twitter推断一些关于追随者数量的报告,但经过大量搜索和反复试验后,我无法获得追随者的数量 - 特别是过去的粉丝数量。
我知道有一个API可以为关注者获取个人userIds,但这对我所需要的东西来说太过分了,我每天都要调用它。理想情况下,如果我可以通过日期并且它可以返回关注者的数量,那将是很好的。
有没有人对此有任何经验以及API可能是什么!
由于
答案 0 :(得分:30)
虽然没有直接的API来获取趋势线,但是通过网址获取关注者数量相当容易:
http://api.twitter.com/1/users/show.json?user_id=12345
文档全部为@ https://dev.twitter.com/docs/api/1/get/users/show
要获得趋势线,似乎我需要每天查询它!
已更新至Twitter API v1.1
https://api.twitter.com/1.1/users/show.json?user_id=12345
https://dev.twitter.com/docs/api/1.1/get/users/show
上的文档2018年5月31日更新
新的API终点位于
https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-show
答案 1 :(得分:16)
虽然您无法直接从Twitter获取此信息,但您可以从Twittercounter.com API获取此信息(您可以注册API密钥here)。
API的速率限制为每小时100次,除非您列入白名单。下面是一个示例请求:
http://api.twittercounter.com?twitter_id=813286&apikey=[api_key]
JSON中的结果如下所示:
{"version":"1.1","username":"BarackObama","url":"http:\/\/www.barackobama.com","avatar":"http:\/\/a1.twimg.com\/profile_images\/784227851\/BarackObama_twitter_photo_normal.jpg","followers_current":7420937,"date_updated":"2011-04-16","follow_days":"563","started_followers":"2264457","growth_since":5156480,"average_growth":"9166","tomorrow":"7430103","next_month":"7695917","followers_yesterday":7414507,"rank":"3","followers_2w_ago":7243541,"growth_since_2w":177396,"average_growth_2w":"12671","tomorrow_2w":"7433608","next_month_2w":"7801067","followersperdate":{"date2011-04-16":7420937,"date2011-04-15":7414507,"date2011-04-14":7400522,"date2011-04-13":7385729,"date2011-04-12":7370229,"date2011-04-11":7366548,"date2011-04-10":7349078,"date2011-04-09":7341737,"date2011-04-08":7325918,"date2011-04-07":7309609,"date2011-04-06":7306325,"date2011-04-05":7283591,"date2011-04-04":7269377,"date2011-04-03":7257596},"last_update":1302981230}
我回答了类似的问题,here。
答案 2 :(得分:1)
这是一个简单的PHP示例,使用CURL,不涉及任何库,使用v2 API和Bearer令牌(bearer令牌是一种访问公共数据的简化方法)来获取所选配置文件(在这里@TwitterFrance)的followers_count通过OAuth 2.0 API)
$authorization = "Authorization: Bearer YOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEAR";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "https://api.twitter.com/2/users/by/username/TwitterFrance?user.fields=public_metrics");
$result = curl_exec($ch);
curl_close($ch);
if (is_string($result)) {
echo (json_decode($result)->data->public_metrics->followers_count);
die();
}
答案 3 :(得分:0)
在Swift 4.2和Xcode 10.1中获得Twitter followers_count
在这里您需要将twitter SDK集成到您的应用中,并遵循集成详细信息https://github.com/twitter/twitter-kit-ios
//This is complete url
https://api.twitter.com/1.1/users/show.json?screen_name=screenName
func getStatusesUserTimeline(accessToken:String) {
let userId = "109*************6"
let twitterClient = TWTRAPIClient(userID: userId)
twitterClient.loadUser(withID: userId) { (user, error) in
print(userId)
print(user ?? "Empty user")
if user != nil {
var request = URLRequest(url: URL(string: "https://api.twitter.com/1.1/users/show.json?screen_name=screenName")!)
request.httpMethod = "GET"
request.setValue("Bearer "+accessToken, forHTTPHeaderField: "Authorization")
print(request)
let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
do {
let response = try JSONSerialization.jsonObject(with: data, options: []) as! Dictionary<String,Any>
print(response)
// print((response["statuses"] as! Array<Any>).count)
} catch let error as NSError {
print(error)
}
}
task.resume()
} else {
print(error?.localizedDescription as Any)
}
}
}