arttuladhar
2/28/2016 - 10:56 AM

Using urllib2 and urllib

Using urllib2 and urllib

import urllib2
import urllib

response = urllib2.urlopen("http://www.aayushtuladhar.com")
print "Response: ", response

print "The URL is ", response.geturl();

# Getting the response Code
print "Response Code: ", response.code

# Getting the Headers
print "Headers: ", response.info()

# Getting all the Data
html = response.read()
print "Getting all Data:", html

# Getting length
print "Get the Length: ", len(html)

####################################
####### Making a Get Call ##########
####################################

url = 'http://til.aayushtuladhar.com'

request = urllib2.Request(url)
response = urllib2.urlopen(request)
html_til = response.read()

print html_til

####################################
####### Making a Post Call ##########
####################################

query_args = { 'q': 'QueryString', 'foo': 'bar'}

data = urllib.urlencode(query_args)

# Send HTTP Post Request
request = urllib2.Request(url, data)
#response = urllib2.urlopen(request)
#html_til = response.read()

#print html_til