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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度