Ich habe Twitter4j verwendet, um Millionen von Tweets zu crawlen, aber in meiner Überraschung sind alle Tweets im rohen JSON-Format gespeichert. Hier ist ein Beispiel für eine formatierte Zeile:Pars Raw Json Strings
StatusJSONImpl{
createdAt=TueNov0119: 00: 04CET2016,
id=793512948027326464,
text='RT @DylanYamaha_: Et profitez vraiment des personnes qui sont près de vous, parce que sa arrive très très vite un malheur..',
source='<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>',
isTruncated=false,
inReplyToStatusId=-1,
inReplyToUserId=-1,
isFavorited=false,
isRetweeted=false,
favoriteCount=0,
inReplyToScreenName='null',
geoLocation=null,
place=null,
retweetCount=0,
isPossiblySensitive=false,
lang='fr',
contributorsIDs=[
],
retweetedStatus=StatusJSONImpl{
createdAt=TueNov0118: 38: 05CET2016,
id=793507418244313088,
text='Et profitez vraiment des personnes qui sont près de vous, parce que sa arrive très très vite un malheur..',
source='<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>',
isTruncated=false,
inReplyToStatusId=-1,
inReplyToUserId=-1,
isFavorited=false,
isRetweeted=false,
favoriteCount=56,
inReplyToScreenName='null',
geoLocation=null,
place=null,
retweetCount=175,
isPossiblySensitive=false,
lang='fr',
contributorsIDs=[
],
retweetedStatus=null,
userMentionEntities=[
],
urlEntities=[
],
hashtagEntities=[
],
mediaEntities=[
],
symbolEntities=[
],
currentUserRetweetId=-1,
user=UserJSONImpl{
id=2242998313,
name='_',
screenName='DylanYamaha_',
location='Marseille, France',
description='null',
isContributorsEnabled=false,
profileImageUrl='http://pbs.twimg.com/profile_images/793110090727424002/9bLOivem_normal.jpg',
profileImageUrlHttps='https://pbs.twimg.com/profile_images/793110090727424002/9bLOivem_normal.jpg',
isDefaultProfileImage=false,
url='null',
isProtected=false,
followersCount=12357,
status=null,
profileBackgroundColor='ABB8C2',
profileTextColor='333333',
profileLinkColor='89C9FA',
profileSidebarFillColor='DDEEF6',
profileSidebarBorderColor='FFFFFF',
profileUseBackgroundImage=false,
isDefaultProfile=false,
showAllInlineMedia=false,
friendsCount=87,
createdAt=ThuDec1223: 15: 28CET2013,
favouritesCount=6007,
utcOffset=3600,
timeZone='Amsterdam',
profileBackgroundImageUrl='http://abs.twimg.com/images/themes/theme1/bg.png',
profileBackgroundImageUrlHttps='https://abs.twimg.com/images/themes/theme1/bg.png',
profileBackgroundTiled=false,
lang='fr',
statusesCount=2049,
isGeoEnabled=false,
isVerified=false,
translator=false,
listedCount=43,
isFollowRequestSent=false,
withheldInCountries=null
},
withHeldInCountries=null,
quotedStatusId=-1,
quotedStatus=null
},
userMentionEntities=[
UserMentionEntityJSONImpl{
name='_',
screenName='DylanYamaha_',
id=2242998313
}
],
urlEntities=[
],
hashtagEntities=[
],
mediaEntities=[
],
symbolEntities=[
],
currentUserRetweetId=-1,
user=UserJSONImpl{
id=393519159,
name='Tiphaine.',
screenName='LehmannTiphaine',
location='France',
description='Snapchat : tiphainelehmann | IG : tiphainelmn',
isContributorsEnabled=false,
profileImageUrl='http://pbs.twimg.com/profile_images/777174096958332928/yoz2aPp2_normal.jpg',
profileImageUrlHttps='https://pbs.twimg.com/profile_images/777174096958332928/yoz2aPp2_normal.jpg',
isDefaultProfileImage=false,
url='null',
isProtected=false,
followersCount=145,
status=null,
profileBackgroundColor='000000',
profileTextColor='333333',
profileLinkColor='000000',
profileSidebarFillColor='F3F3F3',
profileSidebarBorderColor='000000',
profileUseBackgroundImage=true,
isDefaultProfile=false,
showAllInlineMedia=false,
friendsCount=200,
createdAt=TueOct1819: 17: 04CEST2011,
favouritesCount=3202,
utcOffset=3600,
timeZone='Paris',
profileBackgroundImageUrl='http://pbs.twimg.com/profile_background_images/753348262/9d241c29a193586d5dc519838bded4c9.jpeg',
profileBackgroundImageUrlHttps='https://pbs.twimg.com/profile_background_images/753348262/9d241c29a193586d5dc519838bded4c9.jpeg',
profileBackgroundTiled=true,
lang='fr',
statusesCount=3462,
isGeoEnabled=true,
isVerified=false,
translator=false,
listedCount=3,
isFollowRequestSent=false,
withheldInCountries=null
},
withHeldInCountries=null,
quotedStatusId=-1,
quotedStatus=null
}
Ich hatte viele Probleme beim Parsen dieser JSON-Zeichenfolgen. Das erste Problem ist, dass einige Tweets in mehr als einer Zeile vorkommen. Ich habe das gelöst, indem ich die Zeilen desselben Tweets verkettet habe. Dann muss ich alle StatusJSONImpl
und UserJSONImpl
Vorkommen durch "" ersetzen, um den JSONObject
Konstruktor zu verwenden. Das Hauptproblem ist, wie man alle Tweetattribute erhält. Früher habe ich:
JSONObject jsonObj = new JSONObject(jsonline); //from twitter4j.JSONObject;
aber immer dann, wenn der Tweet Text enthält Zeichen wie "'", ich habe:
twitter4j.JSONException: Erwarteter a', 'oder '}' bei 117 [118 Zeichen Linie 1]
Auch wenn der Text kein "'" enthält, konnte ich die Zeiterzeugung (createdAt) des Tweets noch nicht extrahieren. Also habe ich einen String:
String statusFromRaw = "{\"filter_level\": \"low\",";
statusFromRaw+= "\"retweeted\":"+jsonObj.get("isRetweeted")+",";
.....
um eine sauberere Status neu zu erstellen, und verwenden Sie das:
Status status = TwitterObjectFactory.createStatus(statusFromRaw);
Aber ich habe immer noch Probleme in vielen Attributen Parsen, die in den meisten Fällen gleich null sind. Gibt es irgendwelche Vorschläge?
'StatusJSONImpl' ist nicht das rohe JSON, Sie müssen zuerst Ihr Statusobjekt in ein json mit' TwitterObjectFactory.getRawJSON (status) transformieren; ' – FeanDoe
Ich habe es bereits versucht, und ich hatte diese Ausnahme:' Ausnahme im Thread "main" java.lang.IllegalStateException: Anscheinend ist jsonStoreEnabled nicht auf "true" gesetzt. Es ist zu spät, um es auf "true" zu setzen, weil es nur einmal gemacht werden sollte, wenn TwitterObjectFactory aufgerufen wird. – Bill
Ich denke, ich sollte reguläre Ausdrücke verwenden, um die Attribute einzeln zu extrahieren ... – Bill