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

  • R for Data Science Cookbook
  • Yu Wei Chiu (David Chiu)
  • 522字
  • 2021-07-14 10:51:27

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

主站蜘蛛池模板: 丹东市| 五大连池市| 安仁县| 内丘县| 乌什县| 罗定市| 隆昌县| 天水市| 白城市| 信丰县| 厦门市| 图木舒克市| 广水市| 益阳市| 综艺| 庆云县| 谢通门县| 明水县| 石棉县| 德令哈市| 呼伦贝尔市| 金乡县| 苏州市| 菏泽市| 昭平县| 仁怀市| 盈江县| 壶关县| 渝中区| 临海市| 城口县| 广州市| 修水县| 聊城市| 云霄县| 慈利县| 黔江区| 墨脱县| 庆安县| 长岭县| 新巴尔虎左旗|