praw-dev / praw

PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.
http://praw.readthedocs.io/
BSD 2-Clause "Simplified" License
3.47k stars 456 forks source link

Bad Request Error (400 HTTP response) when accessing Reddit API #1939

Closed Auditormadness9 closed 1 year ago

Auditormadness9 commented 1 year ago

Describe the Bug

I am using PRAW library to access Reddit API in my Python project so that I could collect top 100 posts of the selected subreddits. However, I am encountering a Bad Request error (400 HTTP response) when trying to get the posts from a subreddit using the code. The error is in the Logs section.

Desired Result

It should crawl top 100 posts of each of the subreddits specified.

Code to reproduce the bug

import requests
import praw

client_id = <it's properly filled>
client_secret = <it's properly filled>
user_agent = <it's properly filled>
username = <it's properly filled>
password = <it's properly filled>

def get_posts(subreddit):
    reddit = praw.Reddit(client_id=client_id, client_secret=client_secret, user_agent=user_agent, username=username, password=password)

    posts = []
    for submission in reddit.subreddit(subreddit).hot(limit=100):
        posts.append(submission.title + " " + submission.selftext)
    return posts

def save_posts_to_file(posts, subreddit):
    with open(f'{subreddit}.txt', 'w') as file:
        for post in posts:
            file.write(post + "\n")

# Get posts for each subreddit
subreddits = ["r/infinitewarfare", "r/MadeMeSmile", "r/Tinder", "r/Bumble", "r/worldnews"]
for subreddit in subreddits:
    posts = get_posts(subreddit)
    save_posts_to_file(posts, subreddit)

The Reddit() initialization in my code example does not include the following parameters to prevent credential leakage:

client_secret, password, or refresh_token.

Relevant Logs

File "C:\Users\armen\Desktop\getreddits.py", line 26, in <module>
    posts = get_posts(subreddit)
  File "C:\Users\armen\Desktop\getreddits.py", line 14, in get_posts
    for submission in reddit.subreddit(subreddit).hot(limit=100):
  File "C:\Users\armen\AppData\Local\Programs\Python\Python310\lib\site-packages\praw\models\listing\generator.py", line 63, in __next__
    self._next_batch()
  File "C:\Users\armen\AppData\Local\Programs\Python\Python310\lib\site-packages\praw\models\listing\generator.py", line 89, in _next_batch
    self._listing = self._reddit.get(self.url, params=self.params)
  File "C:\Users\armen\AppData\Local\Programs\Python\Python310\lib\site-packages\praw\util\deprecate_args.py", line 43, in wrapped
    return func(**dict(zip(_old_args, args)), **kwargs)
  File "C:\Users\armen\AppData\Local\Programs\Python\Python310\lib\site-packages\praw\reddit.py", line 634, in get
    return self._objectify_request(method="GET", params=params, path=path)
  File "C:\Users\armen\AppData\Local\Programs\Python\Python310\lib\site-packages\praw\reddit.py", line 739, in _objectify_request
    self.request(
  File "C:\Users\armen\AppData\Local\Programs\Python\Python310\lib\site-packages\praw\util\deprecate_args.py", line 43, in wrapped
    return func(**dict(zip(_old_args, args)), **kwargs)
  File "C:\Users\armen\AppData\Local\Programs\Python\Python310\lib\site-packages\praw\reddit.py", line 941, in request
    return self._core.request(
  File "C:\Users\armen\AppData\Local\Programs\Python\Python310\lib\site-packages\prawcore\sessions.py", line 330, in request
    return self._request_with_retries(
  File "C:\Users\armen\AppData\Local\Programs\Python\Python310\lib\site-packages\prawcore\sessions.py", line 266, in _request_with_retries
    raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.BadRequest: received 400 HTTP response

This code has previously worked as intended.

No

Operating System/Environment

Windows 11

Python Version

3.10

PRAW Version

7.6.1

Prawcore Version

2.3.0

Anything else?

No response

LilSpazJoekp commented 1 year ago

This is either an error with your credentials or reddit is having issues. Does this happen with every request or intermittently?

Watchful1 commented 1 year ago

You need to remove the r/ from the front of each subreddit in the list. Just "infinitewarfare" instead of "r/infinitewarfare".

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity.

Remove the Stale label or comment or this will be closed in 30 days.

github-actions[bot] commented 1 year ago

This issue was closed because it has been stale for 30 days with no activity.