Get Twitter Feed as JSON without authentication

前端 未结 8 576
难免孤独
难免孤独 2021-01-30 18:16

I wrote a small JavaScript a couple of years ago that grabbed a users (mine) most recent tweet and then parsed it out for display including links, date etc.

It used this

8条回答
  •  难免孤独
    2021-01-30 18:48

    Here is a quick hack (really a hack, should be used with caution as its not future proof) which uses http://anyorigin.com to scrape twitter site for the latest tweets.

    http://codepen.io/JonOlick/pen/XJaXBd

    It works by using anyorigin (you have to pay to use it) to grab the HTML. It then parses the HTML using jquery to extract out the relevant tweets.

    Tweets on the mobile site use a div with the class .tweet-text, so this is pretty painless.

    The relevant code looks like this:

    $.getJSON('http://anyorigin.com/get?url=mobile.twitter.com/JonOlick&callback=?', function(data){
    
      // Remap ... utf8 encoding to ascii. 
      var bar = data.contents;
      bar = bar.replace(/…/g, '...');
    
      var el = $( '
    ' ); el.html(bar); // Change all links to point back at twitter $('.twitter-atreply', el).each(function(i){ $(this).attr('href', "https://twitter.com" + $(this).attr('href')) }); // For all tweets $('.tweet-text', el).each(function(i){ // We only care about the first 4 tweets if(i < 4) { var foo = $(this).html(); $('#test').html($('#test').html() + "
    " + foo + "

    "); } }); });

提交回复
热议问题