【问题标题】:Getting the text of the tweet only having tweet id without Elevated access to Twitter developer account获取只有推文 ID 的推文文本,而没有提升对 Twitter 开发人员帐户的访问权限
【发布时间】:2022-01-04 03:21:49
【问题描述】:

我尝试使用 tweepy 和 TwitterAPI,但这两种解决方案都不起作用,因为我拥有 Twitter 开发者帐户的基本访问权限,并且没有提升权限(提升权限的应用程序被拒绝)。

基本上在做:

  • tweepy:
# authorization of consumer key and consumer secret

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
# set access to user's access key and access secret 
auth.set_access_token(access_token, access_token_secret)
  
#  calling the api 
api = tweepy.API(auth)
  
# the ID of the status
id = 1272771459249844224
  
# fetching the status
status = api.get_status(id)
  
#  fetching the text attribute

text = status.text 
  • 推特 API:
r = api.request('statuses/show/:%d' % 210462857140252672)

print(r.text)

这两种解决方案都不起作用。我认为所有包含访问密钥的解决方案都行不通。

我也尝试了一些网络抓取,但它也不起作用。这是我尝试过的:

from lxml import html

import requests

page = requests.get('https://twitter.com/UniteAlbertans/status/572342978255048705')

print(page.text)

tree = html.fromstring(page.content)

tree.xpath('//span[@class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0"]/text()')

但我认为这不起作用,因为当我打印页面文本时,我什至看不到推文的文本。

有人可以帮我如何通过代码获取给定推文 ID 的推文文本吗?

非常感谢:)

【问题讨论】:

  • 请尝试使用您在实际代码中使用的相同 python 语法(纯文本)再次编辑问题。这些行// 是无效的python 语法
  • @chickitychinachinesechicken 完成
  • 运行我使用print(status.text) 的tweepy 代码并得到Which movie did you watch recently? Reply us! . . . . . #tuesdaymood #TuesdayThoughts #Bollywood #Hollywood #genre #movies #movie #fun
  • 我无权访问 Twitter Developer Elevated 帐户,我只有必要的访问权限。我收到一条错误消息: TweepError: [{'message': '您当前拥有基本访问权限,其中仅包括对 Twitter API v2 端点的访问权限。如果您需要访问此端点,则需要通过 Developer Portal 申请提升访问权限。您可以在这里了解更多信息:developer.twitter.com/en/docs/twitter-api/getting-started/…', 'code': 453}]。我申请了提升访问权限,但被拒绝了 :( 是的,这是预期的输出
  • 嗯,好吧,我对此一无所知,看来这可能不是编码问题,而更像是 Twitter 开发者帐户问题...

标签: python web-scraping twitter tweepy twitterapi-python


【解决方案1】:

这适用于 Tweepy 和 v2 Twitter API(文档here):

Client.get_tweet(id)

【讨论】:

【解决方案2】:

如果您有基本访问权限,那么您可以只使用 Tweepy Twitter API v2 方法。所以一般情况下,你无法访问tweepy.API,而是必须找到tweepy.Client 的方法。

要通过推文 ID 获取推文的文本,请尝试:

import tweepy

# token for twitter developers
client = tweepy.Client(
    bearer_token="<bearer_token>",
    consumer_key="<consumer_key>",
    consumer_secret="<consumer_secret>",
    access_token="<access_token>",
    access_token_secret="<access_token_secret>",
)

# find tweet by id
tweet = client.get_tweet(id=<tweet_id_here>)

# extract data from tweet
text = tweet.data
print(text)

请确保您已安装最新版本的 tweepy。就我而言,我有 tweepy 版本 4.4.0。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-15
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 2012-12-12
    • 2015-08-27
    相关资源
    最近更新 更多