首页 > 解决方案 > 如何抓取/下载具有特定标签的所有 tumblr 图像

问题描述

我正在尝试从 tumblr 下载许多(1000 张)带有特定标签(例如 #art)的图像。我试图找出最快和最简单的方法来做到这一点。我已经考虑将 scrapy 和 puppeteer 作为选项,并且我阅读了一些关于 tumblr API 的信息,但我不确定如何使用 API 在本地下载我想要的图像。目前,puppeteer 似乎是最好的方法,但我不确定如何处理 tumblr 使用延迟加载的事实(例如,获取所有图像、向下滚动、等待图像加载和获取的代码是什么?这些)将不胜感激任何提示!

标签: web-scrapingscrapypuppeteertumblrpytumblr

解决方案


我的解决方案如下。由于我不能使用偏移量,所以我使用每个帖子的时间戳作为偏移量。由于我试图专门获取帖子中的图像链接,因此我也对输出进行了一些处理。然后我使用一个简单的 python 脚本从我的链接列表中下载每个图像。我已经包含了一个网站和一个额外的堆栈溢出帖子,我发现它们很有帮助。

import pytumblr

def get_all_posts(client, blog):
    offset = None

    for i in range(48):
        #response = client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True)

        response = client.tagged('YOUR TAG HERE', limit=20, before=offset)
        for post in response:
            #    for post in response:
            if('photos' not in post):
                #print(post)
                if('body' in post):
                    body = post['body']
                    body = body.split('<')
                    body = [b for b in body if 'img src=' in b]
                    if(body):
                        body = body[0].split('"')
                        print(body[1])
                        yield body[1]
                    else:
                        yield
            else:
                print(post['photos'][0]['original_size']['url'])
                yield post['photos'][0]['original_size']['url']

        # move to the next offset
        offset = response[-1]['timestamp']
    print(offset)

client = pytumblr.TumblrRestClient('USE YOUR API KEY HERE')

blog = 'staff'

# use our function
with open('{}-posts.txt'.format(blog), 'w') as out_file:
    for post in get_all_posts(client, blog):
        print(post, file=out_file)

链接:

https://64.media.tumblr.com/9f6b4d8d15caffe88c5877cd2fb31726/8882b6bec4975045-23/s540x810/49586f5b05e8661d77e370845d01b34f0f5f2ca6.png

从 Tumblr API 打印 20 多个帖子

也非常感谢原田,他的建议帮了大忙!


推荐阅读