dongnan1989 2012-07-20 22:26
浏览 63
已采纳

有没有办法从Twitter API请求中获取链接,主题标签和@names?

I'm building a website, and I'm using the Twitter API to display data from a user's tweets. It works fine, but all the tweets are retrieved in plain text. This means that, unlike on the Twitter website, all links are simply plain text, no @names are links, and hashtags are completely static.

I would assume that Twitter pulls out these elements using regular expressions, but not only am I fairly poor at regexes, I want the result to be as close to Twitter's implementation as possible. Is there any way to pull these from the Twitter API itself? If not, how could I get parsing as close to Twitter's as possible?

  • 写回答

2条回答 默认 最新

  • douhuxi4145 2012-07-22 05:06
    关注

    Look at Tweet Entities . You can add the parameter &tweet_entities=1 to the end of some REST calls. The JSON response will include the extra attributes your looking for attributed to the tweet.

    I.E

    The urls entity

    An array of URLs extracted from the Tweet text. Each URL entity comes with the following attributes: url , display_url, expanded_url, indices

     "text": "Twitter for Mac is now easier and faster, and you can open multiple windows at once http://t.co/0JG5Mcq",
        "entities": {
          "media": [
          ],
          "urls": [
            {
              "url": "http://t.co/0JG5Mcq",
              "display_url": "blog.twitter.com/2011/05/twitte…",
              "expanded_url": "http://blog.twitter.com/2011/05/twitter-for-mac-update.html",
              "indices": [
                84,
                103
              ]
            }
          ],
          "user_mentions": [
          ],
          "hashtags": [
          ]
        }
    

    The hashtags entity

    An array of hashtags extracted from the Tweet text. Each Hashtag entity comes with the following attributes:

    text
    The Hashtag text indices
    The character positions the Hashtag was extracted from

        "text": "Loved #devnestSF"
    >     "entities": {
    >       "media": [
    >       ],
    >       "urls": [
    >       ],
    >       "user_mentions": [
    >       ],
    >       "hashtags": [
    >         "text": "devnestSF"
    >         "indices": [
    >           6,
    >           16
    >         ]
    >       ]
    >     }
    

    The user_mentions entity

    An array of Twitter screen names extracted from the Tweet text. Each User entity comes with the following attributes:

    id
    The User ID (int format) id_str The User ID (string format) screen_name
    The User screen name name
    The User's full name indices
    The character positions the User mention was extracted from

    "text": "@rno Et demi!"
        "entities": {
          "media": [
          ],
          "urls": [
          ],
          "user_mentions": [
            {
              "id": 22548447,
              "id_str": "22548447",
              "screen_name": "rno",
              "name": "Arnaud Meunier",
              "indices": [
                0,
                4
              ]
            }
          ],
          "hashtags": [
          ]
        }
    

    more Tweet Entities at this link:

    https://dev.twitter.com/docs/tweet-entities

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,