官术网_书友最值得收藏!

Working with twitteR

In addition to obtaining social network interaction data, one can collect millions of tweets from Twitter for further text mining tasks. The method for retrieving data from Twitter is very similar to Facebook. For both social platforms, all we need is an access token to access insight data. After we have retrieved the access token, we can then use twitteR to access millions of tweets.

Getting ready

In this recipe, you need to prepare your environment with R installed and a computer that can access the Internet.

How to do it…

Perform the following steps to read data from Twitter:

  1. First, you need to log in to Twitter and access the page of Twitter Apps at https://apps.twitter.com/. Click on Create New App:

    Figure 26: Creating a new Twitter app

  2. Fill in all required application details to create a new application:

    Figure 27: Filling in the required details

  3. Next, you can select Keys and Access Tokens and then access Application Settings:

    Figure 28: Copying API key and secret

  4. Click on the Create my access token button, and the explorer will generate an authorized access token and the secret:

    Figure 29: Creating the access token and secret

  5. Install and load the twitteR package:
    > install.packages("twitteR")
    > library(twitteR)
    
  6. Set up Twitter OAuth with the copied consumer key and consumer secret from Application Settings, and the copied access token and access secret from Your Access Token:
    > consumer_key <- '<consumer_key>'
    > consumer_secret <- '<consumer_secret>'
    > access_token <- '<access_token>'
    > access_secret <- '<access_secret>'
    > setup_twitter_oauth(consumer_key,
    + consumer_secret,
    + access_token,
    + access_secret)
    [1] "Using direct authentication"
    Use a local file to cache OAuth access credentials between R sessions?
    1: Yes
    2: No
    
    Selection: 1
    Adding .httr-oauth to .gitignore
    
  7. At this point, you can use the searchTwitter function to extract the top 100 search results with world cup as the search key:
    > res <- searchTwitter("world cup", n=100)
    

How it works…

In this recipe, we use twitteR to obtain tweets from Twitter. Similar to Facebook, we need an access token before accessing any tweets. To apply an access token, one must first create an application with one login account and then fill in all the required information to create a new app.

After the app is created, we select the Keys and Access Tokens tab and find both consumer keys and secrets under a section labeled Application Settings. Scroll down further to the button named Create my access token. Clicking on this provides an access token and access token secrets under a section labeled Your Access Token.

At this point, we can now connect Twitter with twitteR. First, install and load the twitteR package. You can then copy the consumer key and consumer secret from Application Settings, and the access token and access secret from Your Access Token. This copied information can be used to set up Twitter OAuth. Finally, we can use the searchTwitter function to find the top 100 search results for the world cup keyword.

There's more…

Similar to Facebook, Twitter also provides its users a console for API testing. You can access the API console from https://dev.twitter.com/rest/tools/console:

Figure 30: Exploring the Twitter API

主站蜘蛛池模板: 安阳市| 巴里| 安泽县| 亚东县| 彭山县| 沙洋县| 杭州市| 黔南| 昌吉市| 安龙县| 政和县| 镇赉县| 隆化县| 科技| 平果县| 大名县| 滁州市| 洛浦县| 宜阳县| 涞水县| 连州市| 西和县| 海宁市| 龙里县| 樟树市| 基隆市| 桐乡市| 吉安市| 花垣县| 留坝县| 沧州市| 玉龙| 高雄市| 祥云县| 喀喇沁旗| 天台县| 四子王旗| 北辰区| 南郑县| 乡宁县| 苍溪县|