search
Search for Tweets
Syntax
Description
searches Tweets for the term d
= search(c
,tweetquery
)tweetquery
.
searches Tweets using web service query parameters. The Twitter® REST API defines web service query parameters.d
= search(c
,tweetquery
,parameters
)
specifies web service query parameters as one or more pairs of name-value
arguments.d
= search(c
,tweetquery
,QueryName1,QueryValue1,...,QueryNameN,QueryValueN
)
Examples
Search for Tweets
Use a Twitter connection object to search for Tweets.
Create a Twitter connection using your credentials. (The values in this example do not represent real Twitter credentials.)
consumerkey = 'abcdefghijklmnop123456789'; consumersecret = 'qrstuvwxyz123456789'; accesstoken = '123456789abcdefghijklmnop'; accesstokensecret = '123456789qrstuvwxyz'; c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret);
Check the Twitter connection. If the StatusCode
property has the value
OK
, the connection is successful.
c.StatusCode
ans = OK
Search for Tweets using the Twitter connection object and the search term bat365
.
tweetquery = 'bat365';
d = search(c,tweetquery)
d = ResponseMessage with properties: StatusLine: 'HTTP/1.1 200 OK' StatusCode: OK Header: [1×25 matlab.net.http.HeaderField] Body: [1×1 matlab.net.http.MessageBody] Completed: 0
d
is a matlab.net.http.ResponseMessage
object. The
StatusCode
property shows OK
, indicating a
successful HTTP request.
Access bat365® Tweets. Display the 12th Tweet.
d.Body.Data.statuses{12}.text
ans = 'MATLAB Control Systems Examples https://t.co/g2P86srv33'
You can search for other Tweets using the search
function. To
retrieve other Twitter data, use the getdata
function.
Search for Specific Number of Tweets Using Structure
Use a Twitter connection object to search for 20 Tweets. Specify the number of Tweets to retrieve as a structure.
Create a Twitter connection using your credentials. (The values in this example do not represent real Twitter credentials.)
consumerkey = 'abcdefghijklmnop123456789'; consumersecret = 'qrstuvwxyz123456789'; accesstoken = '123456789abcdefghijklmnop'; accesstokensecret = '123456789qrstuvwxyz'; c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret);
Check the Twitter connection. If the StatusCode
property has the value
OK
, the connection is successful.
c.StatusCode
ans = OK
Specify the search term bat365
in the variable
tweetquery
. Specify 20 Tweets as a field in the
structure parameters
. Search for 20 Tweets using the
Twitter connection object, search term tweetquery
,
and structure parameters
.
tweetquery = 'bat365';
parameters.count = 20;
d = search(c,tweetquery,parameters)
d = ResponseMessage with properties: StatusLine: 'HTTP/1.1 200 OK' StatusCode: OK Header: [1×25 matlab.net.http.HeaderField] Body: [1×1 matlab.net.http.MessageBody] Completed: 0
d
is a matlab.net.http.ResponseMessage
object. The
StatusCode
property shows OK
,
indicating a successful HTTP request.
Access bat365 Tweets. Display the structure Data
.
d.Body.Data
ans = struct with fields: statuses: {20×1 cell} search_metadata: [1×1 struct]
The structure Data
contains the field
statuses
. This field is a cell array of structures.
Each structure in the cell array contains information about one Tweet®.
Access all 20 Tweets.
d.Body.Data.statuses{:}
ans = struct with fields: created_at: 'Fri Apr 28 17:51:55 +0000 2017' id: 1.2345e+17 id_str: '123456789101112131' text: 'This collection of over 400 MATLAB examples can help you with #controlsystems, Kalman filters, and more https://t.co/FWndxKLA6l…' truncated: 0 entities: [1×1 struct] metadata: [1×1 struct] source: 'Twitter for iPhone' in_reply_to_status_id: [] in_reply_to_status_id_str: [] in_reply_to_user_id: [] in_reply_to_user_id_str: [] in_reply_to_screen_name: [] user: [1×1 struct] geo: [] coordinates: [] place: [] contributors: [] retweeted_status: [1×1 struct] is_quote_status: 0 retweet_count: 34 favorite_count: 0 favorited: 0 retweeted: 0 possibly_sensitive: 0 lang: 'en' ...
The field text
in each structure contains the text of
one Tweet.
(These values do not represent real Twitter data.)
You can search for other Tweets using the search
function. To retrieve other Twitter data, use the getdata
function.
Search for Specific Number of Tweets Using Name-Value Arguments
Use a Twitter connection object to search for 20 Tweets. Specify the number of Tweets to retrieve as a name-value argument.
Create a Twitter connection using your credentials. (The values in this example do not represent real Twitter credentials.)
consumerkey = 'abcdefghijklmnop123456789'; consumersecret = 'qrstuvwxyz123456789'; accesstoken = '123456789abcdefghijklmnop'; accesstokensecret = '123456789qrstuvwxyz'; c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret);
Check the Twitter connection. If the StatusCode
property has the value
OK
, the connection is successful.
c.StatusCode
ans = OK
Search for 20 Tweets using the Twitter connection object, search term bat365
,
and name-value argument count
.
tweetquery = 'bat365'; d = search(c,tweetquery,'count',20)
d = ResponseMessage with properties: StatusLine: 'HTTP/1.1 200 OK' StatusCode: OK Header: [1×25 matlab.net.http.HeaderField] Body: [1×1 matlab.net.http.MessageBody] Completed: 0
d
is a matlab.net.http.ResponseMessage
object. The
StatusCode
property shows OK
,
indicating a successful HTTP request.
Access bat365 Tweets. Display the structure Data
.
d.Body.Data
ans = struct with fields: statuses: {20×1 cell} search_metadata: [1×1 struct]
The structure Data
contains the field
statuses
. This field is a cell array of structures.
Each structure in the cell array contains information about one Tweet.
Access all 20 Tweets.
d.Body.Data.statuses{:}
ans = struct with fields: created_at: 'Fri Apr 28 17:51:55 +0000 2017' id: 1.2345e+17 id_str: '123456789101112131' text: 'This collection of over 400 MATLAB examples can help you with #controlsystems, Kalman filters, and more https://t.co/FWndxKLA6l…' truncated: 0 entities: [1×1 struct] metadata: [1×1 struct] source: 'Twitter for iPhone' in_reply_to_status_id: [] in_reply_to_status_id_str: [] in_reply_to_user_id: [] in_reply_to_user_id_str: [] in_reply_to_screen_name: [] user: [1×1 struct] geo: [] coordinates: [] place: [] contributors: [] retweeted_status: [1×1 struct] is_quote_status: 0 retweet_count: 34 favorite_count: 0 favorited: 0 retweeted: 0 possibly_sensitive: 0 lang: 'en' ...
The field text
in each structure contains the text of
one Tweet.
(These values do not represent real Twitter data.)
You can search for other Tweets using the search
function. To retrieve other Twitter data, use the getdata
function.
Input Arguments
c
— Twitter connection
twitter
object
Twitter connection, specified as a twitter
object.
tweetquery
— Tweet search term
character vector | string scalar
Tweet search term, specified as a character vector or string scalar.
Example: "bat365"
Data Types: char
| string
parameters
— Web service query parameters
structure
Web service query parameters, specified as a structure. Each parameter is specified as a field in the structure. Set the field to a specific value in the structure. For example, specify the number of Tweets to retrieve:
parameters.count = 20;
The Twitter REST API defines web service query parameters that it accepts as part of an HTTP request.
Data Types: struct
QueryName1,QueryValue1,...,QueryNameN,QueryValueN
— Web service query parameters
name-value pairs
Web service query parameters, specified as one or more pairs of name-value
arguments. A QueryName
argument is a character vector or
string scalar that specifies the name of a query parameter. A
QueryValue
argument is a character vector or string
scalar that specifies the value of the query parameter.
The Twitter REST API defines web service query parameters that it accepts as part of an HTTP request.
Example: 'count',20
retrieves 20 Tweets.
Data Types: char
| string
Output Arguments
d
— Twitter data
matlab.net.http.ResponseMessage
Twitter data, returned as a matlab.net.http.ResponseMessage
object.
To retrieve Twitter data, access properties in d
, for example:
data = d.Body.Data
data = struct with fields: statuses: {50×1 cell} search_metadata: [1×1 struct]
Continue to access the nested structure data
to retrieve Twitter data. For accessing nested structures, see Access Data in Nested Structures.
Limitations
The Twitter REST API
GET search/tweets
endpoint specifies that you can retrieve up to a maximum of 100 Tweets at a time.The Twitter REST API
GET search/tweets
endpoint specifies that you can retrieve up to 7 days of historical Tweets.
Version History
Introduced in R2017b
See Also
Functions
Objects
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other bat365 country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)