Pulling from MediaWiki (and the Wikipedia sites in particular) you are
probably better off using the MediaWiki API. If Python is your tool
of choice, I highly recommend mwclient.
http://mwclient.sf.net/
Trevor,
Normally you get "HTTP Error 403: Forbidden" while fetching wikipedia pages using urllib2.urlopen.
Change the User-Agent string to say me be Mozilla example :
import urllib2
hdrs = { 'User-Agent': "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11" }
#First create the Request object, using the headers argument to change the UserAgent.
req = urllib2.Request("http://en.wikipedia.org/wiki/List_of_United_States_mobile_phone_companies" , headers = hdrs)
#use the Request Object to fetch the page.
fd = urllib2.urlopen(req)
This works for me .
Thanks
Regards
Jitendra Nair.
import urllib2import gzip
# this method is used by the method below
def unzip_response(self, contents):compressedstream = StringIO.StringIO(contents)gzipper = gzip.GzipFile(fileobj=compressedstream)return gzipper.read()#just pass the url to this method it will unzip if needed after reading its Content-Encoding :)def get_url(self, url):'''get_url accepts a URL string and return the server response code, response headers, and contents of the file'''req_headers = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13', 'Referer': 'http://python.org'}request = urllib2.Request(url, headers=req_headers) # create a request object for the URLopener = urllib2.build_opener() # create an opener objectresponse = opener.open(request) # open a connection and receive the http response headers + contents#code = response.code#headers = response.headers # headers objectcontents = response.read() # contents of the URL (HTML, javascript, css, img, etc.)##print "its zipped unzipping it..."contents = self.unzip_response(contents)##print "unzipped :) ..."return str(contents)else:return str(contents)