在变量x
中,我有页面中的所有内容,但我希望单独获取推文文本。我怎么能这样做?
from twitter import *
t = Twitter(auth=OAuth("1865941472-AbdUiX4843PBSkz0LwiLXlbbIPj20w9UQKYg5lY",
"WJ76T7i0PDotsP8C42F74hbhzbtUT5cxV3z9ZbcZCuw",
"lNhLOub6HsRm0sukRuyVA",
"QfwvN94uXX55rJ6b5tOCDwCUTfsHXnfxzxRf1Fgt1k"))
t.statuses.home_timeline()
x = t.search.tweets(q="#pycon")
t.statuses.home_timeline()
print x
答案 0 :(得分:0)
如果您想从时间轴中获取推文:
home = t.statuses.home_timeline()
for i in range(len(home)):
print home[i]['user']['name'] + ' (@' + home[i]['user']['screen_name'] + '): ' + home[i]['text']
你将以这种漂亮的风格获得输出:
username (@userlogin): text of tweet
默认情况下,home_timeline()会返回20条最近的推文。您可以使用home_timeline(count=cnt)
更改所需的推文数量。另一种方法是使用TwitterStream()而不是Twitter()。
如果您想从搜索中获取推文:
query = t.search.tweets(q="#pycon")
for i in range():
print query['statuses'][i]['user']['name']+' (@'+query['statuses'][i]['user']['screen_name'] + ') wrote:', query['statuses'][i]['text']