Gaurav Gupta
Gaurav Gupta

Reputation: 1925

Unable to make a http get request to SO

I have a piece of code in python that can make http get requests. I can successfully make request to URL like http://google.com and download their page. But I can't make a get request to http://stackoverflow.com . It shows HTTP 403 forbidden ERROR. However I can access stackoverflow from my browser. So what could be the reason for this error?

code:

import urllib2
c=urllib2.urlopen('https://stackoverflow.com/')
contents=c.read()
print contents[0:50]

error: HTTPError: HTTP Error 403: Forbidden

Upvotes: 1

Views: 920

Answers (1)

cshu
cshu

Reputation: 5944

Same here, I'm using Python 3.

urllib.request.urlopen('http://stackoverflow.com') failed with HTTP error 403.

I changed User-Agent, and then it worked:

import urllib.request
urllib.request.urlopen(urllib.request.Request('http://stackoverflow.com/',headers={'User-Agent':'Mozilla/5.0'}))

So it seems stackoverflow.com filters requests based on User-Agent, and google.com doesn't do so.

urllib2‘s default user agent string is "Python-urllib/2.6" (on Python 2.6)

Source: https://docs.python.org/2/library/urllib2.html

Upvotes: 6

Related Questions