Search code examples
pythonseleniumwebweb-scrapingtwitter

Script to get image url of a given tweet


Using python, I want to automatically get the image source of a given tweet (example: tweet), in order to show it (the image) in a dashboard. If you click on the image you will get the tweet url + '/photo/1'. However this can not be downloadable. If you right click on the image (get image source). You will get a link to the image url (https://pbs.twimg.com/media/D0t9bz_XgAAl3hD?format=jpg&name=large), but this link can't be guessed for a automated coding. Any suggestion on how to automatically download or get a tweet image source ?


Solution

  • You have the ID of the Tweet, in this case 1102112466283175936

    You can use the Twitter API to get the Tweet object. The image information is in the includes data.

    The API query is via the /2/tweets/:id endpoint, and the path to the image URL is includes.media.url. For example:

    $ twurl -j "/2/tweets/1102112466283175936?media.fields=url&expansions=attachments.media_keys"
    
    {
      "data": {
        "attachments": {
          "media_keys": [
            "3_1102112450588147712"
          ]
        },
        "id": "1102112466283175936",
        "text": "5000 selfies make up this iconic image of @jeremycorbyn in today's @ObserverUK, calling on him to bring courage and leadership to unite @UKLabour MPs behind a #PublicVote on #Brexit. RT to support! [shortened link]"
      },
      "includes": {
        "media": [
          {
            "media_key": "3_1102112450588147712",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/D0t9bz_XgAAl3hD.jpg"
          }
        ]
      }
    }
    

    You could use the Tweepy library to access the API from Python. Other API libraries are available.