我正在抓取用户的推文。我需要用ajax分页显示所有推文。我怎样才能做到这一点?
https://api.twitter.com/1.1/statuses/user_timeline.json
试过
我正在使用上述链接。我听说过max_id
,since_id
,但不知道如何使用它。我尝试使用max_id
和since_id
,然后重复收集。我没有得到任何光标响应。
我的代码
$api_key = urlencode('*********'); // Consumer Key (API Key)
$api_secret = urlencode('***********'); // Consumer Secret (API Secret)
$auth_url = 'https://api.twitter.com/oauth2/token';
// what we want?
$data_username = '********'; // username
$data_count = 1; // number of tweets
$data_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
// get api access token
$api_credentials = base64_encode($api_key . ':' . $api_secret);
$auth_headers = 'Authorization: Basic ' . $api_credentials . "\r\n" .
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8' . "\r\n";
$auth_context = stream_context_create(
array(
'http' => array(
'header' => $auth_headers,
'method' => 'POST',
'content' => http_build_query(array('grant_type' => 'client_credentials',)),
)
)
);
$auth_response = json_decode(file_get_contents($auth_url, 0, $auth_context), true);
$auth_token = $auth_response['access_token'];
// get tweets
$data_context = stream_context_create(array('http' => array('header' => 'Authorization: Bearer ' . $auth_token . "\r\n",)));
$datas = json_decode(file_get_contents($data_url . '?include_rts=true&count=' . $data_count . '&screen_name=' . urlencode($data_username), 0, $data_context), true);
// result - do what you want
print('<pre>');
print_r($datas);
问题
获得分页网址需要传递哪些值?
谢谢。
答案 0 :(得分:0)
Twitter API 响应应包含一个 import json
start = [
{
"2020-02-03T00:04:11.000Z": {
"USD": [
0.38449135,
422507.831002123,
0
]
},
"2020-02-04T00:04:08.000Z": {
"USD": [
0.39151832,
390909.273872433,
0
]
},
"2020-02-05T00:04:16.000Z": {
"USD": [
0.40914842,
498653.042530699,
0
]
}
}
]
def process_data(data):
result = []
for k in data:
result += data[k]
return result
result = []
for item in start:
new_dict = {}
for dct_item in item:
data = process_data(item[dct_item])
truncated = dct_item.split('T')[0]
if truncated in new_dict:
new_dict[truncated] += data
else:
new_dict[truncated] = data
result.append(new_dict)
print(json.dumps(result,indent=4))
变量,可用于后续 API 调用/分页。
有关该主题的更多信息,请参阅 https://developer.twitter.com/en/docs/twitter-api/pagination(这里指的是 v2,因为 v1.1 现已弃用)