[ACCEPTED]-What does urllib2.Request(<url>) do and how do i print/view it-python
The class "Request" you're asking 9 about: http://docs.python.org/library/urllib2.html#urllib2.Request
class urllib2.Request(url[, data][, headers][, origin_req_host][, unverifiable])
This 8 class is an abstraction of a URL request.
The 7 function you actually want to make a request 6 (which can accept a Request
object or wrap one 5 around a URL string you provice) constructing 4 a Request object): http://docs.python.org/library/urllib2.html#urllib2.urlopen
urllib2.urlopen(url[, data][,timeout]) Open 3 the URL url, which can be either a string 2 or a Request object.
Example:
theurl = "www.example.com"
try:
resp = urllib2.urlopen(theurl)
print resp.read()
except IOError as e:
print "Error: ", e
Example 2 (with 1 Request
):
theurl = "www.example.com"
try:
req = urllib2.Request(theurl)
print req.get_full_url()
print req.get_method()
print dir(req) # list lots of other stuff in Request
resp = urllib2.urlopen(req)
print resp.read()
except IOError as e:
print "Error: ", e
urllib2.Request()
looks like a function call, but isn't - it's 8 an object constructor. It creates an object 7 of type Request from the urllib2 module, documented 6 here.
As such, it probably doesn't do anything 5 except initialise itself. You can verify 4 this by looking at the source code, which 3 should be in your Python installation's 2 lib directory (urllib2.py, at least in Python 1 2.x).
If you want to have the constructed URL 1 in the Request
object use :
print(urllib2.Request.get_full_url())
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.