0
点赞
收藏
分享

微信扫一扫

tweepy安装与权限获取

河南妞 2022-03-11 阅读 19

1 权限获取

1.1 注册tweeter API 账户

https://developer.twitter.com/en/docs/twitter-api
在这里插入图片描述
如果没有tweeter账号,会提示先注册账号

登录tweeter账号后根据提示填写申请
tips:

  • 如果是非商业的学术用途,最好一开始绑定的联系邮箱就是学校邮箱,可以增加后续申请成功率,不需要用学校邮箱注册,只要将tweeter的Account Information里邮箱改为学校即可

注册完tweeter API账户,就可以凭此登入https://developer.twitter.com/en/portal/dashboard
在这里插入图片描述
你将需要创建一个project,APP key和token在这个网站内"Projets & Apps"随时可查

1.2 申请 Elevated 功能以实现tweepy

在网站developer.tweeter.portal中,Products\Tweeter API v2 下有Elevated 功能,
在这里插入图片描述
点击申请,需要填写表格,其中内容包括:

  • The core use case, intent, or business purpose for your use of the Twitter APIs.
  • If you intend to analyze Tweets, Twitter users, or their content, share details about the analyses you plan to conduct, and the methods or techniques.
  • If your use involves Tweeting, Retweeting, or liking content, share how you’ll interact with Twitter accounts, or their content.
  • If you’ll display Twitter content off of Twitter, explain how, and where, Tweets and Twitter content will be displayed with your product or service, including whether Tweets and Twitter content will be displayed at row level, or aggregated.
  • 是否用于政府blabla填否

可以预先写好模板,提交后需要一段时间受审,有可能会收到tweeter的确认邮件,让你再确认一遍你的申请。

回复邮件:

  • 可以直接用模板再回复一次,注意邮件内不要包括它提出的任何问题
  • 可以选择在美国时间上午回复,受审会很快
  • 收到"Elevated Access Approved"邮件为成功

2 tweepy安装与运行

2.1 在虚拟环境安装tweepy

Step1 创建虚拟环境

mkdir project_name
cd project_name

Step2 安装与创建虚拟环境

  • 安装虚拟环境module(Python 3 不需要这一步)
pip install virtualenv #Windows Python 2
sudo pip install virtualenv  # Linux & macOS
  • 建立虚拟环境

Python 3:

python -m venv environment_name # Windows
python3 -m venv env  # Linux & macOS

Python 2:

virtualenv environment_name

这会创建一个名为’environment_name’ (或env)的文件夹在 project_name 文件中.

Step3 激活虚拟环境和安装tweepy

environment_name\Scripts\activate  # Windows
. env/bin/activate  # Linux & macOS

虚拟环境激活后,安装tweep

pip install tweepy

2.2 运行第一个tweepy

安装好tweepy后,用虚拟环境打开一个python文件
在这里插入图片描述
测试代码:

import tweepy
print("----1")
# Authenticate to Twitter
CONSUMER_KEY="复制"
CONSUMER_SECRET="你的"
ACCESS_TOKEN="这些"
ACCESS_TOKEN_SECRET="信息"

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) 
print("------2")
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
print("------3")

# Create API object
api = tweepy.API(auth)
print("----4")

Key 和token在https://developer.twitter.com/en/portal/dashboard中可以找到和生成

不报错即为成功

举报

相关推荐

0 条评论