0

I'd like to get the latest posts id from a subreddit. Reddit is have basic api for this. You can get json so i want gives data and decode it but i have a error.

root@archi-sunucu:~/yusuf/www# python3 reddit.py
Traceback (most recent call last):
  File "reddit.py", line 24, in <module>
    json = json.loads(resp.text())
TypeError: 'str' object is not callable
root@archi-sunucu:~/yusuf/www# python3 reddit.py

my code:

url = "https://www.reddit.com/r/" + subreddit + "/" + feed + ".json?sort=" + feed + "&limit=6"

resp = requests.get(url, verify=False)
json = json.loads(resp.text())

print(json["data"]["children"][0]["data"]["id"])

thanks for helps...

2
  • 2
    If it is not callable, don't call it: json.loads(resp.text) Commented Nov 3, 2019 at 15:28
  • 1
    Did you read what I wrote? Commented Nov 3, 2019 at 15:39

1 Answer 1

1

You complained that this expression raises an error:

json.loads(resp.text())

Well, let's break that down into something simpler, so the line number tells us exactly what part of your code is failing.

temp = resp.text()
json.loads(temp)

Now we see that the 2nd line doesn't even execute, it fails in the 1st line attempting to compute something to assign to the temporary variable.

Examine resp and its attribute with tools like help(resp), dir(resp), type(resp.text), repr(resp.text). You will soon learn the .text attribute is a str. That is not a callable function, so python raises an error. Use the value directly, without a call:

json = json.loads(resp.text)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.