- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码当前从给定的 Reddit 子版下载图像,并将它们命名为原始文件名。我希望代码做的是将它们命名为 Reddit 上发布的名称。有人可以帮我吗?我认为这与 Submission.title 有关,但我无法弄清楚。干杯。
import praw
import threading
from requests import get
from multiprocessing.pool import ThreadPool
import os
client_id = 'xxxxxxxxx'
client_secret = 'xxxxxxxxx'
user_agent = 'xxxxxxxxx'
image_directory = 'images'
thread_count = 16
target_subreddit = 'space'
image_count = '10'
order = 'hot'
order = order.lower()
reddit = praw.Reddit(client_id=client_id,
client_secret=client_secret, user_agent=user_agent)
def get_order():
if order == 'hot':
ready = reddit.subreddit(target_subreddit).hot(limit=None)
elif order == 'top':
ready = reddit.subreddit(target_subreddit).top(limit=None)
elif order == 'new':
ready = reddit.subreddit(target_subreddit).new(limit=None)
return ready
def get_img(what):
image = '{}/{}/{}'.format(image_directory,
target_subreddit, what.split('/')[-1])
img = get(what).content
with open(image, 'wb') as f:
f.write(img)
def make_dir():
directory = f'{image_directory}/{target_subreddit}'
if not os.path.exists(directory):
os.makedirs(directory)
def main():
c = 1
images = []
make_dir()
for submission in get_order():
url = submission.url
if url.endswith(('.jpg', '.png', '.gif', '.jpeg')):
images.append(url)
c += 1
if int(image_count) < c:
break
results = ThreadPool(thread_count).imap_unordered(get_img, images)
for path in results:
pass
print('Done')
if __name__ == '__main__':
main()
最佳答案
是的,所以如果您的“url”变量为您提供了正确的网址,那么只需submission.title 即可为您提供标题。您可能会遇到编码问题,因此您可能需要使用 str() 进行转换,或者获取一点 fancier with the encode function.另外,许多文件名中不允许使用某些字符,因此也许可以尝试从标题中删除不允许的字符。
关于python - 使用 Praw 抓取 subreddit 帖子标题并将其用作文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55747537/
是否可以使用 PRAW 检查指定用户是否存在,如果存在,正确的方法是什么?我找不到执行此操作的任何内置函数,因此非常感谢您的帮助。 最佳答案 Arnav 的回答仅适用于旧版本的 PRAW (prior
我想在后台运行一个脚本,它会每隔一小时左右获取 subreddit 数据。现在因为我不想在我的数据库中有重复的条目,我想根据 created_utc 过滤我的搜索结果 这是我目前拥有的: r = pr
我想安装 PRAW,这样我就可以制作 reddit 机器人和其他东西,但是所有的安装指南都让我感到困惑,所以有人可以解释一下如何让新手尽可能友好。我对 Vanilla python 有一些经验。谢谢!
我正在尝试使用 PRAW 来组织活跃在/r/nba 中的用户根据他们的天赋发表的所有评论。如果我有用户名,是否有好的方法来获得用户的天赋? 我的最终目标是对按 NBA 球迷分组的用户评论进行一些文本分
正如标题所说,我无法在 PRAW 中找到回复 Reddit 上的帖子的功能。 到目前为止,我已经编写了一个相当简单的机器人,它可以在某个 subreddit 上寻找新帖子。我的问题是,一旦找到提交,我
所以我试图制作一个从 subreddit 中获取随机帖子 url 的机器人,并希望它检查登录凭据是否有效,所以我想出了以下内容: import praw import pandas as pd imp
当我尝试打印带有表情符号的评论或提交时收到错误消息。我怎样才能忽略并只打印字母和数字? 使用 Praw 进行网页抓取 top_posts2 = page.top(limit = 25) for pos
我正在使用 praw 编写一个机器人,我想知道自帖子发布以来已经过去了多长时间。天真的解决方案是使用datetime import datetime import praw ... """read a
是否有任何方法可以限制 Reddit 上特定帖子的评论探索深度。我们有replace_more_comments,它试图替换尽可能多的more_comments,但我们可以限制这种扩展吗?或者我是否需
编辑:在您开始删除/修改安装之前,请浏览 StvnW 的回答/摘要以确保您正在应用适合您的解决方案。 我已经安装了 python 2.7.5 和 pip [编辑:mac OSX Mountain Li
我正在阅读 doc for get_comments而且我不知道 time 参数的正确输入是什么。我知道默认值是“全部”,但我是输入具体日期,还是必须输入“天”、“周”等词? 假设我想获得用户在一周内
该脚本从 Reddit 上的特定评论中获取信息。它的工作与否取决于给它的永久链接。 import praw from pprint import pprint clipboard = ['permal
我正在尝试开始使用 PRAW,但在使用 login() 时遇到问题。 我有以下代码: import praw r = praw.Reddit('This is a test bot') r.login
我希望能够迭代帖子中的评论,最高可达 n 级别,并记录每个评论及其深度。不过,我认为在 Praw 中没有办法轻松做到这一点。 我想做这样的事情: def get_post_comments(post,
我的目标是从 Reddit 获取顶部提交,将其附加到数组中,然后获取第二个顶部提交、第三个、第四个等等。我使用 place_holder 来获取下一个提交,其中第一次有效,但随后只是循环一遍又一遍地获
我正在尝试扫描特定的 Reddit 子版 block ,以查看评论在热门提交中出现的次数。 我无法得到任何迹象表明它实际上正在读取消息,因为它根本不会打印消息正文。注意:sr = Reddit 子版
我正在尝试为我的测试 subreddit 编写一个机器人,但在运行它之前,我正在 PRAW 中测试提交方法。要提交文本帖子,请使用以下行: r.submit('Subreddit', 'Post Ti
所以我在过滤我的虾的结果时遇到了一些问题。我想在结果中排除诸如([request]、[off topic] 或 [nsfw])之类的关键字。我不想在 tweepy 上发布类似 praw 结果中的帖
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我无法理解这在 praw API 中是否可行:我想获得所有评论中提到关键字(比如“python”)的帖子的列表。似乎搜索功能总是从特定的 subreddit 中调用,如 for submission
我是一名优秀的程序员,十分优秀!