Python推特開發庫tweepy基本操作:Twitter for Python
Tweepy是Twitter官方提供的Python第三方開發庫,簡單好用易學高效!
安裝的流程很簡單,如果你和我一樣的win10_64bit使用者,如果你配置了pip管理器,在你的終端裡輸入
>> pip install tweepy
就會自動安裝好了
我們先來看一個小例子
首先給大家看一下我自己的twitter主頁
我一共發出過三個推文,下面我用一個小程式來列印一下我的推文
#匯入tweepy
import tweepy
#填寫twitter提供的開發Key和secret
consumer_key = 'xxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#提交你的Key和secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
#獲取類似於內容控制程式碼的東西
api = tweepy.API(auth)
#列印我自己主頁上的時間軸裡的內容
public_tweets = api.home_timeline()
for tweet in public_tweets:
print tweet.text
他們在後臺被列印了出來。
可能對於新手來說,還不知道tweepy需要你提供的Key和Secret是什麼東東。
這是twitter針對twitter開發者提過的一種類似與暗號一樣的東東,只要你有自己的twitter帳號就可以在下面的頁面 中進行申請:
https://dev.twitter.com/點選開啟連結
然後你就會得到這四個暗號了!
下面讓我們來做一點更有趣的事
先讓我們來看看“小李子”萊昂納多的twitter主頁
哇,還是那麼的帥,因為瀏覽器的原因我們沒辦法擷取更多的內容,下面我們用程式來處理一下。
#匯入tweepy
import tweepy
#填寫twitter提供的開發Key和secret
consumer_key = 'xxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#提交你的Key和secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
#獲取類似於內容控制程式碼的東西
api = tweepy.API(auth)
#列印其他使用者主頁上的時間軸裡的內容
public_tweets = api.user_timeline('LeoDiCaprio')
for tweet in public_tweets:
print tweet.text
![](https://i.iter01.com/images/3c21b91221d892172a313c939a143536b759a6aeebf8bd52970c9f914826b88b.png)
是不是有點刺激了....
我們再來看看怎麼來通過程式傳送我們的推文
#匯入tweepy
import tweepy
#填寫twitter提供的開發Key和secret
consumer_key = 'xxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#提交你的Key和secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
#獲取類似於內容控制程式碼的東西
api = tweepy.API(auth)
#hello python 傳送到自己的帳號上
api.update_status('hello python')
![](https://i.iter01.com/images/d47700e425135c651bc78af26720e31f1d15c1394367dc03b84fcce46ec5c1b6.png)
再來看看如果用程式去搜尋相關主題的人
#匯入tweepy
import tweepy
#填寫twitter提供的開發Key和secret
consumer_key = 'xxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#提交你的Key和secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
#獲取類似於內容控制程式碼的東西
api = tweepy.API(auth)
#搜尋具有League of Legends(lol英雄聯盟的全稱)的關鍵詞的帳號
for tweet in tweepy.Cursor(api.search,q='League of Legends').items(10):
print('Tweet by: @' + tweet.user.screen_name)
我們來看看搜尋結果的第一個使用者@RekladeT是不是我們要找的lol的玩家?
![](https://i.iter01.com/images/eb9cb09751691e87ee5e8e399fe4937e5626f2bbf848c27a48930e2fce99d2ff.png)
感覺是一個lol遊戲音樂方面的發燒友。。。
怎麼樣是不是很有趣?
更多好玩的功能請參照:
http://docs.tweepy.org/en/v3.5.0/index.html 點選開啟連結
而且這個庫,官方在GitHub上提供了原始碼。。。
相關文章
- python基本操作Python
- python+資料庫(三)用python對資料庫基本操作Python資料庫
- Python3資料庫操作基本類Python資料庫
- Python開發必知的6個基本庫!Python
- python 使用csv的基本操作Python
- Python對excel的基本操作PythonExcel
- 學習python視覺化,matplotlib庫學習,基本操作Python視覺化
- python字典基本認識和操作Python
- Python----Requests庫基本使用Python
- Python程式設計有什麼特點?Python開發學習Python程式設計
- Python用什麼工具好?Python開發工具推薦!Python
- 02python開發之基本運算子Python
- python3之os的基本操作Python
- 推薦一個Dapper擴充套件CRUD基本操作的開源庫APP套件
- Python 操作 SQLite 資料庫PythonSQLite資料庫
- Python操作SQLite資料庫PythonSQLite資料庫
- python操作mongodb資料庫PythonMongoDB資料庫
- 《Python3網路爬蟲開發實戰程式碼》基本庫使用Python爬蟲
- Python-OpenCV —— 基本操作一網打盡PythonOpenCV
- 從零開始的Python學習Episode 7——檔案基本操作Python
- Python常用庫和小眾庫推薦Python
- Python 資料庫騷操作 — RedisPython資料庫Redis
- Python 資料庫騷操作 -- RedisPython資料庫Redis
- Python 資料庫騷操作 -- MongoDBPython資料庫MongoDB
- Python資料庫MongoDB騷操作Python資料庫MongoDB
- Python之 操作 MySQL 資料庫PythonMySql資料庫
- Python操作MongoDB文件資料庫PythonMongoDB資料庫
- 2020Q1-2021Q2 推特(Twitter)研發開支及佔比(附原資料表)
- 有哪些值得推薦的Python開發工具Python
- 【推薦】Python常用的三款開發工具!Python
- python基本操作-檔案、目錄及路徑Python
- Python中集合的概念及基本操作詳解!Python
- python書籍推薦-Python爬蟲開發與專案實戰Python爬蟲
- Python開發常用的庫及模組!Python學習教程Python
- Python3網路爬蟲開發實戰——第3章 基本庫的使用Python爬蟲
- Python武器庫 - 科研中常用的python影像操作 - 影像拼接Python
- python開發例項-python開發案例Python
- Python爬蟲之Selenium庫的基本使用Python爬蟲
- Python操作三大主流資料庫Python資料庫