Henry_yl
Henry_yl

Reputation: 73

How to fix the Forbidden: received 403 HTTP response in praw?

I want to collect some title of posts on reddit to do analysis. Through constant debugging of my code, I can got some title of posts. Suddenly I got a Forbidden 403 when attempting to use PRAW to collect posts. The online explanation is that:" Accessing the page or resource you were trying to reach is absolutely forbidden. In other words, a 403 error means that you don't have access to whatever you're trying to view". Please, tell me what should I do. Thanks

try to add some headers and use time delay

url="https://www.reddit.com"

my_headers=["Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html",
"Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31"
]

def get_content(url,headers):

    randdom_header=random.choice(headers)

    req=urllib.Request(url)
    req.add_header("User-Agent",randdom_header)           
    req.add_header("Host","www.reddit.com")
    req.add_header("Referer","https://www.reddit.com")
    req.add_header("GET",url)

    content=urllib.urlopen(req).read()
    return content

print (get_content(url,my_headers))

Upvotes: 3

Views: 6020

Answers (2)

Marcus Salinas
Marcus Salinas

Reputation: 366

Just as an addition to get to the root of the issue for some of you, I ran into this issue because the subreddit I was trying to connect to had gone private.

So my suggestion would be to first check if the subreddit you are connecting to will allow the account the bot is setup with to access that subreddit.

I think the first answer gives a great response on how to handle it programmatically.

Upvotes: 2

gins123
gins123

Reputation: 91

So, I used to run into this problem a lot, and no matter where I googled, I found no "fix", per se. The easiest way to solve the problem is to just catch the error, and then do something with it. See below.

First, you need to import the right "error", I guess. (I'm not sure how to word it)

from prawcore.exceptions import Forbidden

Then, you can try to something that returns our Forbidden error. Do this with try:. Our code should look something like this:

from prawcore.exceptions import Forbidden

try:
    comment.reply("I can comment here!")

Here, we try to make a comment on some (in our case) imaginary comment object, if we are allowed to comment, ie aren't banned from reddit, or this sub, than the reply will successfully "send". If this isn't the case we will recieve that dreadful (in our case) prawcore.exceptions.Forbidden: received 403 HTTP response

To fix this, we simply need to except xxx: our error. This would look like except Forbidden:. We can then do something if we are forbidden from a certain action. Our final code looks like this:

#importing our error
from prawcore.exceptions import Forbidden

#trying to comment (we may be banned)
try:
    comment.reply("I can comment here!")

#doing something if we can't comment
except Forbidden:
   print(f"We\'ve been banned on r/{comment.subreddit}!"

I hope this helped and I'd love to elaborate further!

Upvotes: 6

Related Questions