【问题标题】:Parse tweet content out of embedded twitter timeline从嵌入式 Twitter 时间线中解析推文内容
【发布时间】:2013-10-09 20:48:41
【问题描述】:

我一直想知道并拼命地研究它,但没有任何成功。有什么办法可以挖掘出推文的内容/文本,将其放入变量或其他东西中 - 供以后使用? 如何指出它似乎超出了我的想象。 它被埋在数百个不同的标签下,时间线在一个 iframe 内,它在文档准备好后加载(我想)。 此外,奇怪的是,一旦我开始指向这个 iframe 内的任何东西(我根本没有触摸时间线代码 - 因为那些会怀疑我弄乱了小部件代码的人)。 任何帮助都会非常有帮助。 谢谢

【问题讨论】:

  • 您是否考虑过使用 twitter API 和一些服务器端技术作为 ASP.Net 或 PHP?
  • 我希望我可以直接从 DOM 中获取内容并使用它,而无需深入了解 API。但现在我很沮丧,这可能是最好的主意。在这个完成之前不会睡觉:)

标签: javascript php twitter


【解决方案1】:

如果你使用 twitter api 非常简单,只需输入 $consumerkey、$consumersecret、$accesstoken、$accesstokensecret(只需 sine up https://dev.twitter.com/docs/auth/sign-twitter 并获取密钥)复制我的代码创建一个类并调用函数 getTweets 函数

    function __construct()
     {
           include_once 'twitterApi/twitteroauth/twitteroauth.php';
           /** set parameter for Twitter Api $consumerkey, $consumersecret, $accesstoken, $accesstokensecret **/
           $this->connection = new TwitterOAuth('xxxxx','xxxx','xxxx-xxx','xxxx');
    }


   function getTweets($twitteruser,$nooftweets=30)
    {   /** fire query by api to get tweets **/
        $tweets = $this->objectToArray($this->connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets));

    /** get all tweets in single array */
    for($i= 0;$i<count($tweets); $i++)
    {
        $create[$i]                 = $tweets[$i]['created_at'];
        $tweet[$i]              = $tweets[$i]['text'];
        $retweetcount[$i]       = $tweets[$i]['retweet_count'];
        $favaritecount[$i]      = $tweets[$i]['favorite_count'];

        /** loop for get multipal #tags **/
        for($j=0;$j<=count($tweets[$i]['entities']['hashtags'])-1;$j++)
        {
            $hastag[$i]             .= $tweets[$i]['entities']['hashtags'][$j]['text'].',';
        }
    }
    return array('Tweet'=>$tweet,'Create'=>$create,'Retweetcount'=>$retweetcount,'Favaritecount'=>$favaritecount,'Hastag'=>$hastag);
}



public function objectToArray($d) {
    if (is_object($d)) {
        // Gets the properties of the given object
        // with get_object_vars function
        $d = get_object_vars($d);
    }

    if (is_array($d)) {
        /*
         * Return array converted to object
        * Using __FUNCTION__ (Magic constant)
        * for recursive call
        */
        return array_map(array($this, 'objectToArray'), $d);
        //$this->d = get_object_vars($d);
    }
    else {
        // Return array
        return $d;
    }
}

【讨论】:

  • 稍作调整后我就让它工作了(需要在这些循环之外声明 $hastag 并用 $hastag[] 替换 $hastag[$i] 因为'未定义的偏移量')。但现在我有一系列漂亮的推文:) 再一次感谢你。
【解决方案2】:

使用 jQuery,$('p.js-tweet-text.tweet-text') 应该可以为您提供页面上的所有推文。

【讨论】:

  • 我已经尝试了所有的人......也许我只是做错了,但是对于初学者来说有一个 jQuery 冲突。在那之后——一切似乎都分崩离析了。最后在数百次“刷新”之后——即使是时间线本身(以最纯粹的形式)也会停止工作几分钟。我对这种方法太笨了-我猜:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多