Need help to figure out urllib2.Request()

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Feb 18 15:26:26 EST 2008


En Mon, 18 Feb 2008 09:21:10 -0200, James Yu <cyu021 at gmail.com> escribi�:

> Hi folks,
>
> I tried to open some web pages with urllib2.Request(url, data, headers),  
> but
> it always give me a 404 error.
> Eg.
> url = 'http://www.whatever.com/somephp.php'
> data = {}
> data['id'] = account
> for i in book2Open:
>     data['book'] = i
>     url_data = urllib.urlencode(data)
>     request = urllib2.Request(url, url_data, headers)
>     response = urllib2.urlopen(request)
>     html = response.read()
> ==> HTTPError: HTTP Error 404: Not Found
>
>
> However, the page is retrievable when I manually put url and data  
> together.
> Eg.
> url = 'http://www.whatever.com/somephp.php'
> data = {}
> data['id'] = account
> for i in book2Open:
>     data['book'] = i
>     url_data = urllib.urlencode(data)
>     full_url = url + '?' + url_data
>     request = urllib2.Request(full_url, "", headers)
>     response = urllib2.urlopen(request)
>     html = response.read()
> ==> works fine
>
> Any idea ?

The first example uses HTTP POST method; the second HTTP GET.
See http://docs.python.org/lib/module-urllib2.html

-- 
Gabriel Genellina




More information about the Python-list mailing list