tags:

views:

112

answers:

6

Looking for a python script that would simply connect to a web page (maybe some querystring parameters).

I am going to run this script as a batch job in unix.

+6  A: 

urllib2 will do what you want and it's pretty simple to use.

import urllib
import urllib2

params = {'param1': 'value1'}

req = urllib2.Request("http://someurl", urllib.urlencode(params))
res = urllib2.urlopen(req)

data = res.read()

It's also nice because it's easy to modify the above code to do all sorts of other things like POST requests, Basic Authentication, etc.

Mark Biek
A: 

What are you trying to do? If you're just trying to fetch a web page, cURL is a pre-existing (and very common) tool that does exactly that.

Basic usage is very simple:

curl www.example.com
Sam DeFabbia-Kane
A: 

You might want to simply use httplib from the standard library.

myConnection = httplib.HTTPConnection('http://www.example.com')

you can find the official reference here: http://docs.python.org/library/httplib.html

entens
+2  A: 

A simple wget called from a shell script might suffice.

Buggabill
+1  A: 

Try this:

aResp = urllib2.urlopen("http://google.com/");
print aResp.read();
NawaMan
+1  A: 

If you need your script to actually function as a user of the site (clicking links, etc.) then you're probably looking for the python mechanize library.

Python Mechanize

rhacer