Get Twitter timeline with python

1.First of all

Since I usually use twitter, I want to post and get the timeline using a program, so I'll leave it here as a reminder to do that.

The following contents are described here. B. Acquisition of timeline B. To see the information contained in a tweet

In addition, the environment etc. is the same as Posting tweets with python, and it is assumed that you have acquired the API key.

2. Get the timeline

To get the timeline, you need the following URL, The timeline that can be acquired changes by changing the parameters.

twitter_access.py


url = "https://api.twitter.com/1.1/statuses/home_timeline.json"
params = {"count":200, #How many tweets to get from the latest(Up to 200)
          "include_entities" : 1, #entity(Image URL, etc.)To include in tweets
          "exclude_replies" : 1, #Whether to include a reply
          }

req = sess.get(url, params=params)
timeline = json.loads(req.text)

This will lexicographically assign tweets to the list named timeline.

If you want to see the tweets one by one, use a for sentence or the like.

twitter_access.py


for tweet in timeline:
    print(tweet["text"])

3. To see the information contained in a tweet

Dictionary-type tweets have the following keys.

'is_quote_status', 'in_reply_to_user_id_str', 'place', 'geo', 'retweet_count', 'id_str', 'id', 'contributors', 'user', 'retweeted', 'text', 'in_reply_to_user_id', 'lang', 'in_reply_to_screen_name', 'truncated', 'source', 'favorite_count', 'created_at', 'entities', 'favorited', 'in_reply_to_status_id_str', 'coordinates', 'in_reply_to_status_id'

You can get various elements with this key. If you use'text' as an example, you can get the body of the tweet as a string. The information of the user who tweeted is stored in user in a dictionary type.

The user key is as follows.

'url', 'profile_background_tile', 'screen_name', 'profile_image_url', 'following', 'has_extended_profile', 'statuses_count', 'profile_background_color', 'profile_sidebar_fill_color', 'contributors_enabled', 'notifications', 'listed_count', 'translator_type', 'id', 'profile_background_image_url', 'profile_image_url_https', 'profile_link_color', 'profile_background_image_url_https', 'favourites_count', 'profile_banner_url', 'friends_count', 'location', 'default_profile_image', 'lang', 'verified', 'follow_request_sent', 'profile_use_background_image', 'profile_text_color', 'geo_enabled', 'protected', 'followers_count', 'description', 'is_translator', 'id_str', 'created_at', 'name', 'entities', 'time_zone', 'profile_sidebar_border_color', 'is_translation_enabled', 'utc_offset', 'default_profile'

Also, sometimes the tweet key has a key called extended_entities. The dictionary type is also stored in this key, and there is another key called media_url in the key called media. Inside this is the URL of the image that accompanies the tweet.

Probably not next.

Recommended Posts

Get Twitter timeline with python
[python] Get Twitter timeline for multiple users
Get date with python
Get country code with python
Twitter graphing memo with Python
Use Twitter API with Python
Get Youtube data with python
Get thread ID with python
Get started with Python! ~ ② Grammar ~
Search twitter tweets with python
Get stock price with Python
Get home directory with python
Get keyboard events with python
Get Alembic information with Python
Get media timeline images and videos with Python + Tweepy
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Get reviews with python googlemap api
Get the weather with Python requests
Get web screen capture with python
Get the weather with Python requests 2
[Python] Get economic data with DataReader
How to get started with Python
Post multiple Twitter images with python
[Small story] Get timestamp with Python
Get Qiita trends with Python scraping
Easily post to twitter with Python 3
Get started with Python in Blender
Get weather information with Python & scraping
[Memo] Tweet on twitter with python
[python] Decompose the acquired Twitter timeline into morphemes with MeCab
Collecting information from Twitter with Python (Twitter API)
Get property information by scraping with python
FizzBuzz with Python3
Scraping with Python
[Note] Get data from PostgreSQL with Python
Statistics with python
twitter on python3
Scraping with Python
Python with Go
[Python] Get the variable name with str
Tweet from python with Twitter Developer + Tweepy
Twilio with Python
Integrate with Python
Play with 2016-Python
Get Twitter Trends
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Get Started with TopCoder in Python (2020 Edition)
Bingo with python
Zundokokiyoshi with python
[Python] Get Python package information with PyPI API
How Python beginners get started with Python with Progete
Let's make a Twitter Bot with Python!
Get Twitter userData
Let's get along with Python # 0 (Environment construction)
Excel with Python
Microcomputer with Python
[Blender x Python] Let's get started with Blender Python !!