Python Tutorial

Friday, July 22, 2011

Python:Url Fetch

Python URL Request Response
Some times when we try to fetch url directly server does not return page content. For this reason we need to request the server first then read the page using this response. A sample code of python request response is given below.
We can also set some others additional parameter as timeout, max redirect so on
import urllib2
def get_url_content(site_url):
    rt=""
    try:
        request = urllib2.Request(site_url) 
        f=urllib2.urlopen(request)
        content=f.read()
        f.close()
    except urllib2.HTTPError, error:
        content=str(error.read())
    return content

0 comments:

Post a Comment