Retrieve only screenn_name from string

Asked 2 years ago, Updated 2 years ago, 42 views

I'd like to get RT-only accounts from tweets.

I tried to delete only RT and blanks using the following method, but how do I set it to get only the characters @mikipddw2on11?
*Tweet

RT @mikipddw2on11: Today's Weather #PRhttps://t.covIC68vSr454545

*If RT@mikipddw2on11: begins with the letter
but RT is not present from another tweet. Let's start writing from today's weather.

code

a=tweet.text.replace('\n', ').replace('RT', ').replace(':', ').split('')[0]

Results

 ['RT@mikipddw2on11' ]

python python3

2022-09-30 21:44

2 Answers

There is a way to match @mikipddw2on11 part with regular expression.For example, how about matching it as @ and : The first part that ends with?

import re

content1="RT@examplename:Today's Weather"
content2="Today's weather"
pattern=re.compile('^RT(@[^:])+):')

result1 = pattern.match(content1)

if result1:
    print(result1.group(1))
else:
    print("Not retweeted")

result2 = pattern.match (content2)

if result2:
    print(result2.group(1))
else:
    print("Not retweeted")

Output

@examplename
It's not a retweet.


2022-09-30 21:44

I don't know what kind of data tweet is, so I may not be able to give you an accurate answer.

Imagine and answer from the official Twitter document.Tweet Object
Assume that the questioner's tweet contains the linked json data.
If you do that, wouldn't you be able to get it below?

json_obj['tweet']['retweeted_status']['user']['screen_name']

Looking at the User Object documentation, it seems that screenn_name corresponds to @.User Object

If you want to take it from tweet.text, I think it would be easier to use split function

tweet.text.split('')[1][1:]


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.