如何使用 tweepy 或任何其他 python 方法获取特定推文的转推计数?

How to get retweet count for a certain tweet using tweepy or any other python method?

是否可以使用 tweepy python 库获取特定推文的转发计数?我想获得从特定推特帐户发布的每条推文的转发计数。是否有任何使用 tweepy 的 python 方法?我试过使用 beautifulsoup。但是我在计算每条推文时遇到了麻烦。它 returns 值仅用于定义的推文 ID。那么我该如何更改此代码以获取来自该 Twitter 帐户的每条已发布推文的值?

        id=[[tweet.id]for tweet in alltweets]


        html = requests.get("https://twitter.com/%s/status/%s" % ("usename", "userID"))
        soup = BeautifulSoup(html.text, 'lxml')

        comments = soup.find_all('span', attrs={'class': 'ProfileTweet-actionCountForAria'})[0].contents

    outtweets = [{'ID': tweet.id_str, 'Text': tweet.text, 'Date': tweet.created_at, 'author': tweet.user.screen_name,
                  'retweet-count': tweet.retweet_count, 'favourites-count': tweet.favorite_count, 'language': tweet.lang,
                  'reply-count': comments}for tweet in alltweets]

下面的代码可以用来解决这个问题。

from urllib.request import urlopen
from bs4 import BeautifulSoup

for page in range(0, 50):
    url = "https://twitter.com/(user.screen.name)".format(page)
    html = urlopen(url)
    soup = BeautifulSoup(html, "html.parser")
    tweets = soup.find_all("li", attrs={"class": "js-stream-item"})

for tweet in tweets:
    try:
        if tweet.find('p',{"class":'tweet-text'}):
            tweet_text = tweet.find('p', {"class": 'tweet-text'}).text.encode('utf8').strip()
            retweets = tweet.find('span', {"class": "ProfileTweet-action--retweet"}).text.strip()
            print(tweet_user, "|", retweets)
    except: AttributeError

首先设置你的 api 并找到你想要计算转推的推文 ID,然后 运行 下面的代码

retweets_list = api.retweets(ID)
print (retweets_list[0].retweet_count)