0

I was following a tutorial on pythonforbeginners.com, and I came across a code which isn't running right on my OSX.

from bs4 import BeautifulSoup
import urllib2
url = "http://www.pythonforbeginners.com"
content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content)
print soup.prettify()

This gives me the error:

Traceback (most recent call last): File "/Users/dhruvmullick/CS/Python/Extracting Data/test.py", line 8, in content = urllib2.urlopen(url).read() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen return _opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in open response = meth(req, response) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 523, in http_response 'http', request, response, code, msg, hdrs) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 448, in error return self._call_chain(*args) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 531, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 403: Forbidden

Dhruv Mullick
  • 551
  • 9
  • 25

1 Answers1

0

The 403 error indicates that the server is blocking your connection.

...a request from a client for a web page or resource to indicate that the server can be reached and understood the request, but refuses to take any further action.

Try with a different domain and you'll find that it works as expected.

To make a work-around, you can add a custom user-agent.

Community
  • 1
  • 1
philshem
  • 24,761
  • 8
  • 61
  • 127