获取给定推文图像 url 的脚本

Script to get image url of a given tweet

使用python,我想自动获取给定推文的图片来源(例如: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),但是这个link无法自动编码猜测.关于如何自动下载或获取推文图像源的任何建议?

你有推文的 ID,在本例中为 1102112466283175936

您可以使用 Twitter API 获取 Tweet 对象。图像信息在includes数据中。

API 查询是通过 /2/tweets/:id 端点进行的,图像 URL 的路径是 includes.media.url。例如:

$ 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"
      }
    ]
  }
}

您可以使用 Tweepy 库从 Python 访问 API。其他 API 个库可用。