Twitter crawler 与sina 微博类似,使用twitter api之前,首先要有twitter的账号,在twitter developer中创建应用(https://apps.twitter.com/app/new)。

创建成功之后可以获得应用的信息,包括Consumer key和Consumer secret。并generate access token,将这四个数据保存下来,

接下来可以借助twitter api 进行twitter 抓取,现有的twitter api的python版本很多,这里主要介绍tweepy 以及python-twitter。

1. python-twitter

安装:在cmd 窗口中使用pip 命令:pip install python-twitter

安装成功后,可以运行如下代码:

import twitter

import optparse
import sys def print_safe(string):
"""
Format a string for safe printing
"""
return string.encode('cp437', 'xmlcharrefreplace') def print_tweet(tweet):
"""
Format and print `tweet`.
"""
print "@" + print_safe( tweet.GetUser().GetScreenName() ) + \
": " + print_safe(tweet.GetText()) def search(search_term):
"""
Print recent tweets containing `search_term`.
"""
api = twitter.Api()
tweets = api.GetSearch(search_term)
for tweet in tweets:
print_tweet(tweet) def trending_topics():
"""
Print the currently trending topics.
"""
api = twitter.Api()
trending_topics = api.GetTrendsCurrent()
for topic in trending_topics:
print print_safe(topic.name) def user_tweets(username):
"""
Print recent tweets by `username`.
"""
api = twitter.Api()
user_tweets = api.GetUserTimeline(screen_name=username)
for tweet in user_tweets:
print_tweet(tweet) def trending_tweets():
"""
Print tweets for all the trending topics.
"""
api = twitter.Api() trending_topics = api.GetTrendsCurrent()
tweets = []
# To add some variety, let's round-robin through the trending
# topics, displaying a tweet from each until we run out of tweets.
for topic in trending_topics:
tweets.append((topic, api.GetSearch(topic.name))) while True:
for topic, topic_tweets in tweets:
if topic_tweets:
print_tweet(topic_tweets.pop())
else:
return def main(args):
parser = optparse.OptionParser("""Usage: %prog [-s <search term> | -t | -u <username>]""") parser.add_option("-s", "--search",
type="string",
action="store",
dest="search_term",
default=None,
help="Display tweets containing a particular string.")
parser.add_option("-t", "--trending-topics",
action="store_true",
dest="trending_topics",
default=False,
help="Display the trending topics.")
parser.add_option("-u", "--user",
type="string",
action="store",
dest="username",
default=None,
help="Display tweets for a particular public user.")
parser.add_option("-w", "--trending-tweets",
action="store_true",
dest="trending_tweets",
default=None,
help="Display the tweets from trending topics.") (opts, args) = parser.parse_args(args) if opts.search_term:
search(opts.search_term)
elif opts.trending_topics:
trending_topics()
elif opts.username:
user_tweets(opts.username)
elif opts.trending_tweets:
trending_tweets() if __name__ == "__main__":
main(sys.argv[1:])
  

PS:有时会遇到程序报错,缺少module,可能系统安装过python-twitter,但是版本较低,因此可以首先卸载twitter-python 再重新安装,卸载命令为:pip uninstall python-twitter。

2.tweepy:

首先tweepy下载:https://github.com/tweepy/tweepy

Twitter search API的更多相关文章

  1. Twitter REST API, Streaming API

    原文链接           用Twitter自己的话来说:   REST API The REST API provides simple interfaces for most Twitter f ...

  2. 【337】Text Mining Using Twitter Streaming API and Python

    Reference: An Introduction to Text Mining using Twitter Streaming API and Python Reference: How to R ...

  3. Search API 官方文档 可以用了查看自己的app

    Search API October 24, 2012 - HTTPS is now supported for Search and Lookup requests. Please update y ...

  4. ElasticSearch Search API 简介

    REST request URI curl 'localhost:9200/bank/_search?q=*&pretty' 1. localhost:9200/bank/_search,以 ...

  5. Elasticsearch学习笔记(二)Search API 与 Query DSL

    一. Search API eg: GET /mall/product/_search?q=name:productName&sort=price desc 特点:search的请求参数都是以 ...

  6. 通过Google Custom Search API 进行站内搜索

    今天突然想把博客的搜索改为google的站内搜索,印象中google adsense中好像提高这个站内搜索的代码,但苦逼的是google adsense帐号一直审核不通过,所以只能通过google c ...

  7. 申请Bing Search API

    地址:https://datamarket.azure.com/home 没有帐号先注册一个,然后登录. 1:在数据中订阅Bing Search API,如果找不到就使用这个地址: https://d ...

  8. Elasticsearch 6.x版本全文检索学习之Search API

    Elasticsearch 6.x版本全文检索学习之Search API. 1).Search API,实现对es中存储的数据进行查询分析,endpoind为_search,如下所示. 方式一.GET ...

  9. Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介)

    原文:Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介) 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...

随机推荐

  1. ios开发@selector的函数如何传参数/如何传递多个参数

    不同的类会有不同的传递方式,参数名也不尽相同.如果是传单个参数的就不用集合,如果是传多个参数可以用类似nsarray,nsdictionary之类的集合传递.看下面例子: 例子1: 通过NSTimer ...

  2. OpenXML_导入Excel到数据库(转)

    (1).实现功能:通过前台选择.xlsx文件的Excel,将其文件转化为DataTable和List集合 (2).开发环境:Window7旗舰版+vs2013+Mvc4.0 (2).在使用中需要用到的 ...

  3. 模板插件aTpl

    摘要: 前面给大家分享了一款js模板插件,后来经过完善推荐给大家.该插件支持ie5+,chrome等浏览器以及移动端浏览器,支持for和if语法,以及表达式. 项目地址:https://github. ...

  4. Linux下更好用的帮助命令—cheat

    导读 Linux系统中,我们经常会用man命令来帮助查看这个命令的具体用法,man是很强大的,但是英语不好的同学用man用起来可能不那么顺手,自然而然的就出现了cheat命令,cheat命令就是通过简 ...

  5. The content of element type "package" must match "(result-types?,interceptors?...

    错误:“The content of element type "package" must match "(result-types?,interceptors?,de ...

  6. 黑客讲述渗透Hacking Team全过程(详细解说)

    近期,黑客Phineas Fisher在pastebin.com上讲述了入侵Hacking Team的过程,以下为其讲述的原文情况,文中附带有相关文档.工具及网站的链接,请在安全环境下进行打开,并合理 ...

  7. puppet之自定义fact(转载)

    1.使用环境变量'FACTERLIB'创建fact 1.1.在自定义目录里面定义一个fact,列出当前系统登录的用户数 [root@agent1 ~]# vim /var/lib/puppet/kis ...

  8. Linux 制作ftp远程yum仓库

    一.下载createrepo yum install createrepo -y 二.安装vsftp软件 yum install vsftpd -y 三.将pub制作为yum仓库 把需要的rpm包拷贝 ...

  9. JAVA程序1,1,2,3,5,8,13,21....第30个是什么...?

    解题思路:从第3个数字开始,后一个数字是前2个数字的和public class text{ public static void main(String[] args) { int num1=1,nu ...

  10. Java for LeetCode 171 Excel Sheet Column Number

    Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...