简体   繁体   English

如何从reddit.com/r/showerthoughts获得随机的热门帖子

[英]How to get random top posts from reddit.com/r/showerthoughts

I have the following code that is supposed to take a random top post from reddit.com/r/showerthoughts and print the title and author of the post. 我有以下代码,应该从reddit.com/r/showerthoughts中随机发一个顶级帖子,并打印该帖子的标题和作者。

import  random, json
randnum = random.randint(0,99)
response = json.load('https://www.reddit.com/r/showerthoughts/top.json?sort=top&t=week&limit=100')["data"]["children"][randnum]["data"]
print("\n\"" + response["title"] + "\"")
print("    -" + response["author"] + "\n")

I get the following error: 我收到以下错误:

Traceback (most recent call last):
  File "C:/Users/jacks/.PyCharmCE2019.1/config/scratches/scratch_4.py", line 4, in <module>
    response = json.load('https://www.reddit.com/r/showerthoughts/top.json?sort=top&t=week&limit=100')["data"]["children"][randnum]["data"]
  File "C:\Users\jacks\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'

Am I on the right track here? 我在正确的轨道上吗?

UPDATE: Got it to work with this code: 更新:可以使用以下代码:

import  random, requests
randnum = random.randint(0,99)
response = requests.get('https://www.reddit.com/r/showerthoughts/top.json?sort=top&t=week&limit=100', headers = {'User-Agent': 'showerbot'})
result = response.json()
result1 = result["data"]["children"][randnum]["data"]
print("\n\"" + result1["title"] + "\"")
print("    -" + result1["author"] + "\n")

You cannot load json directly from a url, for that you need to use requests module. 您不能直接从URL加载json,因为您需要使用requests模块。

Using json module 使用json模块

import  random, json, requests
randnum = random.randint(0,99)

response = requests.get('https://www.reddit.com/r/showerthoughts/top.json?sort=top&t=week&limit=100')

response = json.loads(response.text)
response = response["data"]["children"][randnum]["data"]

print("\n\"" + response["title"] + "\"")
print("    -" + response["author"] + "\n")

Without using json module 不使用json模块

import  random,  requests
randnum = random.randint(0,99)

response = requests.get('https://www.reddit.com/r/showerthoughts/top.json?sort=top&t=week&limit=100')

response = response.json()
response = response["data"]["children"][randnum]["data"]

print("\n\"" + response["title"] + "\"")
print("    -" + response["author"] + "\n")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium Python从reddit.com搜索页面上的问题中提取标题和href属性 - How to extract the title and href attributes from the questions on reddit.com search page using Selenium Python 如何运行reddit.com网站的克隆。 Reddit.com源代码在Ubuntu 9.10上实现时出现错误(karmic) - How to run a clone of reddit.com website. Reddit.com source code gives error while implementing on Ubuntu 9.10 (karmic) Scrapy 为 reddit.com 返回 301 - Scrapy returning 301 for reddit.com 使用 praw 从 reddit 获取前 10 个帖子 - obtaining top 10 posts from reddit using praw 在reddit.com中执行“paster shell example.ini”时出错 - Error while executing “paster shell example.ini” in reddit.com Python Reddit PRAW 获得最高周。 如何更改限制? - Python Reddit PRAW get top week. How to change limit? 从reddit获取顶级壁纸 - Getting the top wallpaper from reddit Django,如何获取随机帖子列表结果? - Django, How to get random posts list result? 如何使用 Python 中的请求从 Reddit 页面的帖子中获取所有图像链接 - How to obtain all image links from a Reddit page's posts using requests in Python 我已经使用他们的API解析了Reddit帖子,如何使用NLTK仅从这些帖子中提取问题? - I have Parsed Reddit posts using their API How can I extract only questions from this posts using NLTK?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM