如何使用tweepy或任何其他python方法获取某条推文的转发数?

是否可以使用tweepy python库获取某条推文的转发数?我想获取从特定Twitter帐户发布的每条推文的转发数。是否有任何使用tweepy的python方法?我试过使用beautifulsoup。但是我很难为每条推文获取计数。它仅返回定义的tweet 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]
sqli182 回答:如何使用tweepy或任何其他python方法获取某条推文的转发数?

以下代码可用于解决此问题。

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,找到你想统计转发次数的tweet ID,然后运行下面的代码

retweets_list = api.retweets(ID)
print (retweets_list[0].retweet_count)
本文链接:https://www.f2er.com/3154425.html

大家都在问