CODE:
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on 2014-7-28
@author: guaguastd
@name: extract_from_arbitrary.py
'''
if __name__ == '__main__':
# import json
import json
# import search
from search import search_for_tweet
# import login, see http://blog.csdn.net/guaguastd/article/details/31706155
from login import twitter_login
# get the twitter access api
twitter_api = twitter_login()
# import twitter_text
import twitter_text
while 1:
query = raw_input('\nInput the query (eg. #MentionSomeoneImportantForYou, exit to quit): ')
if query == 'exit':
print 'Successfully exit!'
break
statuses = search_for_tweet(twitter_api, query)
ex = twitter_text.Extractor(statuses)
screen_names = ex.extract_mentioned_screen_names_with_indices()
urls = ex.extract_urls_with_indices()
hashtags = ex.extract_hashtags_with_indices()
# Explore the first 5 items for each...
print json.dumps(screen_names[0:5], indent=1)
print json.dumps(urls[0:5], indent=1)
print json.dumps(hashtags[0:5], indent=1)
RESULT:
Input the query (eg. #MentionSomeoneImportantForYou, exit to quit): #MentionSomeoneImportantForYou
Length of statuses 32
[
{
"indices": [
68,
78
],
"screen_name": "ggktyssie"
},
{
"indices": [
113,
124
],
"screen_name": "BE_IBGDRGN"
},
{
"indices": [
180,
192
],
"screen_name": "RMP_IBGDRGN"
},
{
"indices": [
2850,
2858
],
"screen_name": "sdrpxx1"
},
{
"indices": [
2886,
2897
],
"screen_name": "BE_IBGDRGN"
}
]
[
{
"url": "http://twitter.com/download/iphone",
"indices": [
327,
361
]
},
{
"url": "https://abs.twimg.com/images/themes/theme1/bg.png",
"indices": [
1161,
1210
]
},
{
"url": "https://pbs.twimg.com/profile_images/493273088721580035/ITsV9jH-_normal.jpeg",
"indices": [
1297,
1373
]
},
{
"url": "http://pbs.twimg.com/profile_images/493273088721580035/ITsV9jH-_normal.jpeg",
"indices": [
1877,
1952
]
},
{
"url": "https://pbs.twimg.com/profile_banners/2673775045/1406340167",
"indices": [
2024,
2083
]
}
]
[
{
"indices": [
149,
179
],
"hashtag": "MentionSomeoneImportantForYou"
},
{
"indices": [
2923,
2953
],
"hashtag": "MentionSomeoneImportantForYou"
},
{
"indices": [
5830,
5860
],
"hashtag": "MentionSomeoneImportantForYou"
},
{
"indices": [
8495,
8525
],
"hashtag": "MentionSomeoneImportantForYou"
},
{
"indices": [
11197,
11227
],
"hashtag": "MentionSomeoneImportantForYou"
}
]
Input the query (eg. #MentionSomeoneImportantForYou, exit to quit):