Tweepy:现在可以通过Twitter搜索API获取旧推文吗?

5
根据http://www.theverge.com/2014/11/18/7242477/twitter-search-now-lets-you-find-any-tweet-ever-sent的报道,Twitter现在可以搜索到曾经发布过的所有推文。
但是当我使用tweepy获取2014年至2015年期间的推文时,它只会返回最近的一些推文。
    query = 'Nivea'
    max_tweets = 1000
    searched_tweets = [json.loads(status.json) for status in tweepy.Cursor(api.search,
                                                                           q=query,
                                                                           count=100,
                                                                           #since_id="24012619984051000",
                                                                           since="2014-02-01",
                                                                           until="2015-02-01",
                                                                           result_type="mixed",
                                                                           lang="en"
                                                                           ).items(max_tweets)]

我尝试使用since="2014-02-01"和since_id,但是无论如何都不行。


Twitter API 有一些限制,这个库解决了这个问题,看看:https://github.com/Jefferson-Henrique/GetOldTweets-python - Jefferson Henrique C. Soares
2个回答

4

很遗憾,您无法访问Twitter的过去数据。 这不是您使用的库的问题:无论是Tweepy、Twitter4J还是其他任何库,都只是因为Twitter不会提供超过大约2周的旧数据。

要获取历史数据,您需要直接通过Twitter或类似GNIP的第三方经销商获得对firehose的访问权限。


1
我使用自己的代码,其中包含一个 HttpURLConnection 和一个 Twitter 搜索 URL。然后我使用正则表达式来提取最后 20 条匹配的推文... 幸运的是,当我删除推文时,我可以简单地再次搜索,直到找不到更多的推文。我包含了代码,虽然它是用 Java 写的,但任何语言都适用。首先,我使用一个类来搜索推文并记录它们的详细信息:
public class ReadSearch{
    private String startURL = "https://twitter.com/search?f=realtime&q=from%3A";
    private String middleURL = "%20%40";
    private String endURL = "&src=typd";

    public ArrayList<Tweet> getTweets(String user, String troll) {
        ArrayList<Tweet> tweets = new ArrayList<Tweet>();
        String expr = "small.class=\"time\".*?href=\"/"
                + "([^/]+)"
                + ".*?status/"
                + "([^\"]+)"
                + ".*?title=\""
                + "([^\"]+)";
        Pattern patt = Pattern.compile(expr, Pattern.DOTALL | Pattern.UNIX_LINES);
        try {
            Matcher m = patt.matcher(getData(startURL+user+middleURL+troll+endURL));
            while (m.find()) {
                if(user.equals(m.group(1).trim())){
                    Tweet tw = new Tweet();
                    tw.setUser(m.group(1).trim());
                    tw.setTweetid(Long.parseLong(m.group(2).trim()));
                    tw.setDate(m.group(3).trim());
                    tweets.add(tw);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception " + e);
        }
        return tweets;
    }

    private StringBuilder getData(String dataurl) throws MalformedURLException, IOException{
        URL url = new URL(dataurl);
        HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
        httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
        StringBuilder sb = new StringBuilder(16384);
        BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream(), "ISO-8859-1"));
        String line;
        while ((line = br.readLine()) != null){
            sb.append(line);
            sb.append('\n');
        }
        httpcon.disconnect();
        br.close();
        return sb;
    }

    public static void main(String [] args){
        //testing
        ReadSearch rs = new ReadSearch();
        ArrayList<Tweet> tweets = rs.getTweets("Tony_Kennah", "PickLuckier");
        for(Tweet t : tweets){
            System.out.println("TWEET: " + t.toString());
        }
    }
}

我们随后需要Tweet类本身,以便我们可以将Tweets分组并对其执行操作,它只是一个像这样的bean:
public class Tweet{ 
    private String user;
    private long tweetid;
    private String date;

    public String getUser(){
        return user;
    }
    public void setUser(String user){
        this.user = user;
    }
    public long getTweetid(){
        return tweetid;
    }
    public void setTweetid(long tweetid){
        this.tweetid = tweetid;
    }
    public String getDate(){
        return date;
    }
    public void setDate(String date){
        this.date = date;
    }
    public String toString(){
        return this.tweetid + " " + this.user + " " + this.date;
    }
}

...所以那只是标准Java。要使用上面的代码,我使用Twitter4J API并执行以下操作:

public class DeleteTweets
{
    public static void main(String args[]) throws Exception
    {
        Twitter twitter = TwitterFactory.getSingleton();
        ArrayList<Tweet> tweets = new ArrayList<Tweet>();
        String [] people = { "PickLuckier" };
        for(String s : people){
            do{
                ReadSearch rs = new ReadSearch();
                tweets = rs.getTweets(twitter.getScreenName(), s);
                for(Tweet tw : tweets){
                    twitter.destroyStatus(tw.getTweetid());
                }
            } while(tweets.size()!=0);
        }
    }
}

这就是全部内容了。我不使用注释,但希望您能清楚地看到正在发生的事情,并且这有助于您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接