如何 scrape/download 所有带有特定标签的 tumblr 图片
How to scrape/download all tumblr images with a particular tag
我正在尝试从带有特定标签(例如#art)的 tumblr 下载许多(1000 张)图片。我试图找出最快和最简单的方法来做到这一点。我考虑过 scrapy 和 puppeteer 作为选项,并且我阅读了一些关于 tumblr API 的内容,但我不确定如何使用 API 在本地下载我想要的图像。
目前,puppeteer 似乎是最好的方法,但我不确定如何处理 tumblr 使用延迟加载的事实(例如,获取所有图像、向下滚动、等待图像加载和获取的代码是什么这些)
非常感谢任何提示!
我建议您使用 Tumblr API,这里有一些关于如何操作的说明。
- 阅读文档的 What You Need 部分
- 阅读 Get Posts With Tag 部分
- 考虑使用像 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,不会撒谎。但是文档保持最新状态。一旦你有了 post 的 HTML,图像的 link 就会在那里。有很多像 Beautiful Soup 这样的库可以通过 CSS 选择器从 HTML 中提取图像。希望这对您有所帮助!
我的解决方案如下。由于我不能使用偏移量,所以我使用每个 post 的时间戳作为偏移量。因为我试图专门获取 post 中的图像链接,所以我也对输出做了一些处理。然后我使用一个简单的 python 脚本从我的链接列表中下载每张图片。我已经包含了一个网站和一个额外的堆栈溢出 post,我发现它很有帮助。
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)
链接:
也非常感谢原田老师,他的建议帮了大忙!
我正在尝试从带有特定标签(例如#art)的 tumblr 下载许多(1000 张)图片。我试图找出最快和最简单的方法来做到这一点。我考虑过 scrapy 和 puppeteer 作为选项,并且我阅读了一些关于 tumblr API 的内容,但我不确定如何使用 API 在本地下载我想要的图像。 目前,puppeteer 似乎是最好的方法,但我不确定如何处理 tumblr 使用延迟加载的事实(例如,获取所有图像、向下滚动、等待图像加载和获取的代码是什么这些) 非常感谢任何提示!
我建议您使用 Tumblr API,这里有一些关于如何操作的说明。
- 阅读文档的 What You Need 部分
- 阅读 Get Posts With Tag 部分
- 考虑使用像 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,不会撒谎。但是文档保持最新状态。一旦你有了 post 的 HTML,图像的 link 就会在那里。有很多像 Beautiful Soup 这样的库可以通过 CSS 选择器从 HTML 中提取图像。希望这对您有所帮助!
我的解决方案如下。由于我不能使用偏移量,所以我使用每个 post 的时间戳作为偏移量。因为我试图专门获取 post 中的图像链接,所以我也对输出做了一些处理。然后我使用一个简单的 python 脚本从我的链接列表中下载每张图片。我已经包含了一个网站和一个额外的堆栈溢出 post,我发现它很有帮助。
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)
链接:
也非常感谢原田老师,他的建议帮了大忙!