自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

學習如何使用Python構(gòu)建你自己的Twitter機器人

開發(fā) 后端 機器人
Twitter 允許用戶將博客帖子和文章分享給全世界。使用 Python 和 Tweepy 庫使得創(chuàng)建一個 Twitter 機器人來接管你的所有的推特變得非常簡單。

 

Twitter 允許用戶將博客帖子和文章分享給全世界。使用 Python 和 Tweepy 庫使得創(chuàng)建一個 Twitter 機器人來接管你的所有的推特變得非常簡單。這篇文章告訴你如何去構(gòu)建這樣一個機器人。希望你能將這些概念也同樣應用到其他的在線服務(wù)的項目中去。

開始

tweepy 庫可以讓創(chuàng)建一個 Twitter 機器人的過程更加容易上手。它包含了 Twitter 的 API 調(diào)用和一個很簡單的接口。

下面這些命令使用 pipenv 在一個虛擬環(huán)境中安裝 tweepy。如果你沒有安裝 pipenv,可以看一看我們之前的文章如何在 Fedora 上安裝 Pipenv

  1. $ mkdir twitterbot
  2. $ cd twitterbot
  3. $ pipenv --three
  4. $ pipenv install tweepy
  5. $ pipenv shell 

Tweepy —— 開始

要使用 Twitter API ,機器人需要通過 Twitter 的授權(quán)。為了解決這個問題, tweepy 使用了 OAuth 授權(quán)標準。你可以通過在 https://apps.twitter.com/ 創(chuàng)建一個新的應用來獲取到憑證。 

創(chuàng)建一個新的 Twitter 應用

當你填完了表格并點擊了“創(chuàng)建你自己的 Twitter 應用Create your Twitter application”的按鈕后,你可以獲取到該應用的憑證。 Tweepy 需要用戶密鑰API Key用戶密碼API Secret,這些都可以在 “密鑰和訪問令牌Keys and Access Tokens” 中找到。

向下滾動頁面,使用“創(chuàng)建我的訪問令牌Create my access token”按鈕生成一個“訪問令牌Access Token” 和一個“訪問令牌密鑰Access Token Secret”。

使用 Tweppy —— 輸出你的時間線

現(xiàn)在你已經(jīng)有了所需的憑證了,打開一個文件,并寫下如下的 Python 代碼。

  1. import tweepy
  2. auth = tweepy.OAuthHandler("your_consumer_key", "your_consumer_key_secret")
  3. auth.set_access_token("your_access_token", "your_access_token_secret")
  4. api = tweepy.API(auth)
  5. public_tweets = api.home_timeline()
  6. for tweet in public_tweets:
  7. print(tweet.text)

在確保你正在使用你的 Pipenv 虛擬環(huán)境后,執(zhí)行你的程序。

  1. $ python tweet.py

上述程序調(diào)用了 home_timeline 方法來獲取到你時間線中的 20 條最近的推特。現(xiàn)在這個機器人能夠使用 tweepy 來獲取到 Twitter 的數(shù)據(jù),接下來嘗試修改代碼來發(fā)送 tweet。

使用 Tweepy —— 發(fā)送一條推特

要發(fā)送一條推特 ,有一個容易上手的 API 方法 update_status 。它的用法很簡單:

  1. api.update_status("The awesome text you would like to tweet")

Tweepy 拓展為制作 Twitter 機器人準備了非常多不同有用的方法。要獲取 API 的詳細信息,請查看文檔。 

一個雜志機器人

接下來我們來創(chuàng)建一個搜索 Fedora Magazine 的推特并轉(zhuǎn)推這些的機器人。

為了避免多次轉(zhuǎn)推相同的內(nèi)容,這個機器人存放了最近一條轉(zhuǎn)推的推特的 ID 。 兩個助手函數(shù) store_last_idget_last_id 將會幫助存儲和保存這個 ID。

然后,機器人使用 tweepy 搜索 API 來查找 Fedora Magazine 的最近的推特并存儲這個 ID。

  1. import tweepy
  2.  
  3. def store_last_id(tweet_id):
  4. """ Stores a tweet id in text file """
  5. with open('lastid', 'w') as fp:
  6. fp.write(str(tweet_id))
  7.  
  8.  
  9. def get_last_id():
  10. """ Retrieve the list of tweets that were
  11. already retweeted """
  12.  
  13. with open('lastid') as fp:
  14. return fp.read()
  15.  
  16. if __name__ == '__main__':
  17.  
  18. auth = tweepy.OAuthHandler("your_consumer_key", "your_consumer_key_secret")
  19. auth.set_access_token("your_access_token", "your_access_token_secret")
  20.  
  21. api = tweepy.API(auth)
  22.  
  23. try:
  24. last_id = get_last_id()
  25. except FileNotFoundError:
  26. print("No retweet yet")
  27. last_id = None
  28.  
  29. for tweet in tweepy.Cursor(api.search, q="fedoramagazine.org", since_id=last_id).items():
  30. if tweet.user.name == 'Fedora Project':
  31. store_last_id(tweet.id)
  32. #tweet.retweet()
  33. print(f'"{tweet.text}" was retweeted')

為了只轉(zhuǎn)推 Fedora Magazine 的推特 ,機器人搜索內(nèi)容包含 fedoramagazine.org 和由 「Fedora Project」 Twitter 賬戶發(fā)布的推特。 

結(jié)論

在這篇文章中你看到了如何使用 tweepy 的 Python 庫來創(chuàng)建一個自動閱讀、發(fā)送和搜索推特的 Twitter 應用?,F(xiàn)在,你能使用你自己的創(chuàng)造力來創(chuàng)造一個你自己的 Twitter 機器人。

這篇文章的演示源碼可以在 Github 找到。

責任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2017-08-21 13:31:44

AI聊天機器人facebook

2021-09-10 08:54:28

Twitter機器人賬號

2019-01-25 16:30:34

機器人機器學習人工智能

2024-08-06 08:40:32

2024-09-30 13:11:09

2021-07-22 10:17:55

加密機器人加密貨幣機器人

2021-05-07 13:20:39

Python機器人編程語言

2017-03-28 12:21:21

機器人定義

2020-10-15 15:42:00

人工智能

2021-11-06 10:53:07

機器學習機器人AI

2015-11-03 13:50:21

SlackDocker運維機器人

2017-07-07 14:41:13

機器學習神經(jīng)網(wǎng)絡(luò)JavaScript

2021-11-02 09:40:50

TensorFlow機器學習人工智能

2015-12-10 21:49:32

IM機器人

2021-09-03 16:12:52

機器人人工智能編程

2022-05-13 16:07:01

機器人養(yǎng)老人工智能

2021-02-05 10:18:07

深度學習機器人通用人工智能

2020-12-31 06:55:37

機器人自然語言人工智能

2024-10-18 16:50:00

機器人特斯拉

2016-06-02 11:45:34

點贊
收藏

51CTO技術(shù)棧公眾號