我正在学习在Tweepy中使用Twitter API。我需要提取原始Tweet数据的帮助-这意味着没有缩短的URL。例如,This Tweet显示一个YouTube链接,但在被API解析后,会显示一个t.co链接。如何打印显示的文字?感谢您的帮助。
注意:我与this question有类似的关注点,但是并不相同。
功能代码:
def get_tweets(username):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
# Call api
api = tweepy.API(auth)
tweets = api.user_timeline(screen_name=username)
# Empty Array
tmp=[]
# create array of tweet information: username,
# tweet id, date/time, text
tweets_for_csv = [tweet.text for tweet in tweets] # CSV file created
for j in tweets_for_csv:
# Append tweets to the empty array tmp
tmp.append(j)
dict1 = {}
punctuation = '''`~!@#$%^&*(){}[];:'".,\/?'''
tmps = str(tmp)
for char in tmps:
if char in punctuation:
tmps = tmps.replace(char," ")
tmps2 = tmps.split(" ")
a = 0
while a < len(tmps2):
for b in tmps2:
dict1[a] = b
a += 1
答案 0 :(得分:0)
Twitter的API会返回原始Tweet数据,而不进行任何解析。该数据包括缩短的URL,因为这就是表示Tweet的方式。 Twitter本身只是解析并显示原始URL。链接本身甚至仍然是缩短的链接。
Tweet objects具有一个entities
属性,该属性为entities object提供了一个urls
字段,该字段是URL objects的数组,表示包含在Tweet的文本,如果没有链接,则为空数组。每个URL对象包括一个display_url
字段和一个indices
字段,原始URL粘贴/键入到Tweet中,而Type "help", "copyright", "credits" or "license" for more information.
>>> python -i notebook.py
File "<stdin>", line 1
python -i notebook.py
^
SyntaxError: invalid syntax
>>>
字段是一个整数数组,表示URL起始和结束的Tweet文本中的偏移量。您可以使用这些字段替换缩短的URL。