web-scraping - 如何抓取/下载具有特定标签的所有 tumblr 图像
问题描述
我正在尝试从 tumblr 下载许多(1000 张)带有特定标签(例如 #art)的图像。我试图找出最快和最简单的方法来做到这一点。我已经考虑将 scrapy 和 puppeteer 作为选项,并且我阅读了一些关于 tumblr API 的信息,但我不确定如何使用 API 在本地下载我想要的图像。目前,puppeteer 似乎是最好的方法,但我不确定如何处理 tumblr 使用延迟加载的事实(例如,获取所有图像、向下滚动、等待图像加载和获取的代码是什么?这些)将不胜感激任何提示!
解决方案
我的解决方案如下。由于我不能使用偏移量,所以我使用每个帖子的时间戳作为偏移量。由于我试图专门获取帖子中的图像链接,因此我也对输出进行了一些处理。然后我使用一个简单的 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)
链接:
也非常感谢原田,他的建议帮了大忙!
推荐阅读
- memory-management - alloc_calls 和 free_calls 是什么意思?
- php - 在 Wordpress 页脚中垂直对齐自定义创建的菜单
- amazon-web-services - Echo Spot 有时需要几分钟才能开始播放视频
- spring-boot - 如何对我的独立(非 Web 服务)应用程序进行 Spring 化?
- python - Python ping 脚本
- angular - 您如何访问 Angular 组件中的属性指令以获取规范?
- html - 哪个 div 的宽度是固定的?
- delphi - Delphi 7 到 XE8 分配二进制 ADO 参数值
- vbscript - MailItem.SentOn 在收件箱中的项目上生成 Object 不支持此属性或方法
- azure - USQL 日志记录、调试和故障排除