Jarek
Jarek

Reputation: 351

How to get content received by urllib2 with 403 Forbidden

How can I get with urllib2 content of page sent with 403 Forbidden ? In real browser I'm receiving customized 403 page, but urllib2 throws exception.

Upvotes: 0

Views: 61

Answers (1)

ojii
ojii

Reputation: 4781

I strongly suggest you use requests instead of urllib2 if possible, it will make your life better.

Anyway since you asked for urllib2 specifically, here's how:

try:
    response = urllib2.urlopen('http://www.example.com')
except urllib2.HTTPError as exc:
    if exc.code == 403:
        content = exc.read()

The content variable will hold the HTML source of the 403 page.

Upvotes: 1

Related Questions