【问题标题】:Script to get image url of a given tweet获取给定推文的图像 url 的脚本
【发布时间】:2021-12-07 09:20:36
【问题描述】:

使用 python,我想自动获取给定推文的图像源(例如:tweet),以便在仪表板中显示它(图像)。如果您单击图像,您将获得推文 url + '/photo/1'。但是,这不能下载。如果您右键单击图像(获取图像源)。您将获得一个指向图像 url (https://pbs.twimg.com/media/D0t9bz_XgAAl3hD?format=jpg&name=large) 的链接,但对于自动编码,无法猜测此链接。关于如何自动下载或获取推文图像源的任何建议?

【问题讨论】:

    标签: python selenium web web-scraping twitter


    【解决方案1】:

    您有推文的 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 库可用。

    【讨论】:

      猜你喜欢
      • 2012-04-30
      • 2019-08-17
      • 2011-08-11
      • 2012-11-01
      • 2019-10-25
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多