【问题标题】:How to scrape/download all tumblr images with a particular tag如何抓取/下载所有带有特定标签的 tumblr 图像
【发布时间】:2020-12-13 11:00:57
【问题描述】:

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

【问题讨论】:

  • 您可能希望使用 API 来检索图像的链接,然后使用另一个程序将这些图像下载到您的计算机。该API实际上相当易于使用,并且可以进行您想要的大规模收集。
  • 谢谢原田!我从来没有使用过 tumblr API,你能给我更具体的说明吗?

标签: web-scraping scrapy puppeteer tumblr pytumblr


【解决方案1】:

我建议你使用 Tumblr API,所以这里有一些关于如何去做的说明。

  1. 阅读文档的What You Need 部分
  2. 阅读Get Posts With Tag 部分
  3. 考虑使用 PyTumblr 之类的库
import pytumblr

list_of_all_posts = []

# Authenticate via OAuth
client = pytumblr.TumblrRestClient(
    'YOUR KEY HERE'
)

def get_art_posts():
    posts = client.tagged('art', **params) # returns HTML of 20 most recent posts in the tag
    # use params (shown in tumblr documentation) to change the timestamp of limit of the posts
    # i.e. to only posts before a certain time
    return posts

 list_of_all_posts.append(get_art_posts())

我对 Tumblr API 很生疏,不会撒谎。但是文档保持最新。获得帖子的 HTML 后,图像的链接将在那里。有很多库,比如 Beautiful Soup,可以通过他们的 CSS 选择器从 HTML 中提取图像。希望这有帮助!

【讨论】:

  • 谢谢原田!这很有帮助。我实际上已经开始研究 pytumblr 实现,但我无法获得超过 20 个帖子 - 似乎 client.tagged 没有偏移参数?当我尝试添加偏移量时,出现“偏移量不允许字段”错误。你知道如何解决这个问题吗?
  • @gollyzoom 我很高兴 pytumblr 为你工作。似乎您也自己处理了 20 个帖子的限制。玩得开心!
【解决方案2】:

我的解决方案如下。由于我不能使用偏移量,所以我使用每个帖子的时间戳作为偏移量。由于我试图专门获取帖子中的图像链接,因此我也对输出进行了一些处理。然后我使用一个简单的 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

Print more than 20 posts from Tumblr API

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2011-12-18
    • 2015-10-04
    相关资源
    最近更新 更多